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.
std::string::c_str
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.
strlen
size_t strlen ( const char * 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 100, strlen(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;