【C++】函数笔记

一、strtod

将字符串转换成浮点数,表头文件是#include <stdlib.h>,相关函数有atoi,atol,strtod,strtol。

strtod()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时('\0')才结束转换,并将结果返回。若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分。如123.456或123e-2。

#include<stdlib.h>
#include<stdio.h>
void main()
{
    char *endptr;
    char a[] = "12345.6789";
    char b[] = "1234.567qwer";
    char c[] = "-232.23e4";
    printf( "a=%lf\n", strtod(a,NULL) );
    printf( "b=%lf\n", strtod(b,&endptr) );
    printf( "endptr=%s\n", endptr );
    printf( "c=%lf\n", strtod(c,NULL) );
}
a=12345.678900
b=1234.567000
endptr=qwer
c=-2322300.000000
 

二、atof

double atof(const char *str)
把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
   float val;
   char str[20];
   
   strcpy(str, "98993489");
   val = atof(str);
   printf("字符串值 = %s, 浮点值 = %f\n", str, val);
 
   strcpy(str, "runoob");
   val = atof(str);
   printf("字符串值 = %s, 浮点值 = %f\n", str, val);
 
   return(0);
}

 

三、memcpy

内存拷贝函数。功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。

所在头文件 <string.h>或<cstring>

int main()
{
    char a[4] = "mmmm";
    char b[7] = "123455";
    memcpy(b,a,3);
    printf("%d\n\r",sizeof(b));
    printf("%s\n",b);
    for(int i = 0; i < sizeof(b); i++)
        printf("b[%d]的字符串是%c\n\r",i,b[i]);
    return 0;
}

https://blog.csdn.net/qq_26747797/article/details/82794333

 

四、strcmp

比较参数1和参数。
1、若参数1>参数2,返回正数;
2、若参数1<参数2,返回负数;
3、若参数1=参数2,返回0

 

五、CString类常用方法

CString Left(int nCount) const;               //从左边1开始获取前 nCount 个字符
CString Mid(int nFirst) const;                //从左边第 nFirst+1 个字符开始,获取后面所有的字符
CString Mid(int nFirst, int nCount) const;    //从左边第 nFirst+1 个字符开始,获取后面  nCount 个字符
CString Right(int nCount) const;              //从右边1开始获取从右向左前 nCount 个字符

 MFC中CString.Format的用法

 

注:
在函数后面加 const 的意思是:
如果一个类声明了一个常量对象,这个对象只能使用后边带 const 这个的方法.

例:
CString a,b;
a = "123456789";

b = a.Left(4);   //值为:1234
b = a.Mid(3);    //值为:456789
b = a.Mid(2, 4); //值为:3456
b = a.Right(4);  //值为:6789

https://www.cnblogs.com/yangxx-1990/p/4881158.html

 

六、memset

memset 函数是内存赋值函数,用来给某一块内存空间进行赋值的;
包含在<string.h>头文件中,可以用它对一片内存空间逐字节进行初始化。

https://www.cnblogs.com/yhlboke-1992/p/9292877.html

 

七、fopen和fopen_s

打开和写入文件。

https://blog.csdn.net/weixin_40893646/article/details/103755006

 

八、

posted @ 2021-12-20 17:46  不溯流光  阅读(95)  评论(0编辑  收藏  举报