Something you should know in C++

1.
function
<cstdlib>

atoi

int atoi (const char * str);

Convert string to integer

Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int.

The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.

2.

public member function
<string>

std::string::c_str

const char* c_str() const noexcept;
Get C string equivalent
Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the  string object.

This array includes the same sequence of characters that make up the value of the  string object plus an additional terminating null-character ( '\0') at the end.

3.

function
<cstring>

strlen


size_t strlen ( const char * str );
Get string length
Returns the length of the C string  str.

The length of a C string is determined by the terminating null-character: A  C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

This should not be confused with the size of the array that holds the string. For example:

char mystr[100]="test string"; 

defines an array of characters with a size of 100  chars, but the C string with which  mystr has been initialized has a length of only 11 characters. Therefore, while  sizeof(mystr) evaluates to  100strlen(mystr) returns  11.

In C++, char_traits::length implements the same behavior.


4.  std::vector<char>转换string

(1)   std::string data;
/*for( unsigned int i = 0; i < buffer->size(); i++)
{
data += (*buffer)[i];
}*/

(2)   std::string data;

data.assign((*buffer).begin(), (*buffer).end());


5. string 转换 char*

std::string data = "abc";

char *p = data.c_str();


6. 字符串相加

(1)

char *char1 = "edab"; 
char *char2 = "dfrey"; 
string str1(char1);
string str2(char2);
string str3;
str3 = str1 + str2;

(2)

char A[30] = "test";
char B[4] = "add"; 
strcat(A,B);//A要有足够空间扩展B中内容

(3)

char *a = "tttt";
char *b = "dddd";
char *dest = new char[strlen(a)+strlen(b)+1];
strcpy(dest,a);
strcat(dest,b);
cout << dest;
delete []dest;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值