C++ string对象
#include<string>
using std::string;
string s1;
string s2(s1);
string s2 = s1;
string s3("value");
string s3 = "value";
string s4 = string("value")
string s5(10,'c');
cout << s
cin >> s
getline(cin,s)
s.empty()
s.size()
s[n]
s1 + s2
s1 = s2
s1 == s2
s1 != s2
< , <= , > , >=
- string的size_type类型
string的size()函数返回的类型是string::size_type类型,它是一个无符号类型的值。
string的下标,比如str[0]下标0的类型也是string::size_type类型。
string line = "abcd";
auto len = line.size();
- 使用范围for语句(auto)来遍历string的每一个元素
string str("some string");
for(auto c : str)
cout<<c<<endl;
C风格字符串
- 头文件为C标准库文件string.h或对应的C++版本cstring
#include<cstring>
#include<string.h>
char *a = "hello";
char a[] = "hello";
char a[] = {'h','e','l','l','o','