1. Function declaration
C++ string member function length() is the same as size(), but strlen() is different from C library function. First take a look at the declaration of the three functions:
//返回string长度,单位字节
size_t length() const noexcept;
//返回string长度,单位字节。作用等同于length()
size_t size() const noexcept;
//C标准库函数,返回C风格字符串长度,单位字节
size_t strlen ( const char * str );
2. Use example
in real projects, when C++ string gets the length, we often use the following two methods.
//方法一:调用length()或size()
string strTest="test";
strTest.length(); //结果为4
strTest.size(); //结果为4
//方法二:转为C风格字符串,调用strlen()
strlen(strTest.c_str()); //结果为4
the above code snippet takes a string length of 4 and makes no difference, so what’s the difference between method 1 and method 2?See the following code:
char buf[256]={0};
buf[0]='a';
buf[2]='v';
buf[3]='h';
string strTest(buf,6);
cout<<"strTest[0]:"<<(uint32_t)strTest[0]<<"_"<<(uint32_t)strTest[1]<<"_"<<(uint32_t)strTest[2]<<"_"<<(uint32_t)strTest[3]<<"_"<<(uint32_t)strTest[4]<<"_"<<(uint32_t)strTest[5]<<endl;
cout<<"strTest.length()="<<strTest.length()<<" strTest.size()="<<strTest.size()<<" strlen(strTest.c_str())="<<strlen(strTest.c_str())<<endl;
cout<<"strTest:"<<strTest<<endl;
output:
strTest[0]:97_0_118_104_0_0
strTest.length()=6 strTest.size()=6 strlen(strTest.c_str())=1
strTest:avh
Conclusion
3. h1>
(1) when a string contains the null character ‘\0’, the length of the string is truncated using strlen(), and the member functions length() and size() are used to return the true length of the string.
(2) cout will filter out null characters when it outputs string, and the output will not be truncated.
(3) when constructing or stitching a string, it is recommended to specify the length of the string at the same time, such as
//构造时使用
string strTest(buf,6);
//而非,因为会被截断
string strTest(buf);
//拼接时使用
strTest.append(buf,6);
//而非,因为会被截断
strTest+=buf;
reference
[1] C++ reference
Read More:
- C language string processing error warning, c4996, sprintf, predicted, c4996, strcpy, c4996, strcat
- The usage and difference of atoi() and stoi() functions in C + +
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)
- Solve the problem of error: cannot pass objects of non trivially copyable type ‘STD:: String’ in C / C + +
- [C + +] C + + overload operator = must be a nonstatic member function?
- C++ string substr()
- In C + + cin.getline The difference between () and getline () functions
- C++ foundation — clear/erase/pop of string class_back
- A repeated string is composed of two identical strings. For example, abcabc is a repeated string with length of 6, while abcba does not have a duplicate string. Given any string, please help Xiaoqiang find the longest repeated substring.
- C++:error C2228: left of ‘.str’ must have class/struct/union
- C++: terminate called after throwing an instance of ‘std::length_error‘ (sort function cmp sorting rules problem)
- C / C + + rounding function ceil(), floor ()
- C + + programming fault handling — error: assignment of read only data member ‘STD:: pair
- Error c2137 of C language: empty character constant (Fixed)
- C# Member XXX cannot be accessed with an instance with an instance reference;qualify it with a type
- C / C + + library function (tower / tower) realizes the conversion of letter case
- Differences between shell script variable assignment and C language variable assignment
- Solution: regarding c2039, XXX is not a member of XXX
- error: ‘to_string’ is not a member of ‘std’
- Error c2065: ‘new’: undeclared identifier, mainly because it is a. C file