机试漫谈

一、string 转 int

头文件“stdlib.h”

atoi

这个函数是把char * 转换成int的。应该是属于标准库函数。在想把string 转换成int的时候,须要下面流程:

不能直接printf string,string为拓展类,链接错误。printf只能输出C的内置数据。

string -> char * -> int

string a = "1234";
int b = atoi(a.c_str());  //c_str();转char []

itoa

功能是int->char *,所以能够:

itoa(int a,char *b[],int c)  //a数据,b char[],c进制。

二、int写入char []
itoa在不能用的情况下,可用
snprintf(buffer, sizeof(buffer), "%d", i)



二、char数组复制
char * strcpy( char *strDest, const char *strSrc )
{
 assert( (strDest != NULL) && (strSrc != NULL) );
 char *address = strDest;
 while( (*strDest++ = * strSrc++) != ‘\0’ );
 return address;
}

在库函数中,字符的赋值所采用的循环代码,只用了一行代码:while( (*strDest++ = * strSrc++) != ‘\0’ );

 

三、int []逆序

reverse(a,a+n);

posted @ 2019-01-23 00:23  会飞的雅蠛蝶  阅读(86)  评论(0编辑  收藏  举报