场景
1.std::string 我们经常用来存储字符串数据, 当然它也可以作为byte的存储器,存储任意字节.
2.通常情况下我们使用 std::string 的 compare 方法比较字符串, 但这个方法比较奥字符串是不可靠的.
说明
1.compare 方法和 strcmp并不相同, 它比较的是 std::string size()大小里的所有字节.在size() 长度范围里, 如果有’\0’字符, 一样进行比较, 所有在不知道 std::string里是否存储纯字符串时, 最好先转换为 const char* (调用c_str()), 再调用 strcmp比较. 这个坑还是很吓人的.
例子
1.以下例子很好的说明在比较字符串时 compare并不适合.
#include <string>
#include <iostream>
#include <string.h>
int main(
int argc,
char const *argv[]){
std::
string str(
"hello")