-
初始化string 对象的方式
string s1; 默认构造函数 s1 为空串 string s2(s1); 将 s2 初始化为 s1 的一个副本 string s3(“value”); 将 s3 初始化为一个字符串字面值副本 string s4(n, ‘c’); 将 s4 初始化为字符 ‘c’ 的 n 个副本 -
string 对象的读写
• 读取并忽略开头所有的空白字符(如空格,换行符,制表符)。
• 读取字符直至再次遇到空白字符,读取终止。
1、读取一行int main() { string s; // empty string cin >> s; // read whitespace-separated string into s //输入hello world cout << s << endl; // write s to the output //输出hello return 0; }
2、可以把多个读操作或多个写操作放在一起
string s1, s2; cin >> s1 >> s2; // read first input into s1, second into s2 //输入 hello world cout << s1 << s2 << endl; // write both strings //输出 helloworld
3、读入未知数目的string 对象
string 的输入操作符也会返回所读的数据流,因此,可以把输入操作作为判断条件int main() { string word; // read until end-of-file, writing each word to a new line while (cin >> word) cout << word << endl; return 0; }
4、使用getline 读取整行文本
getline 并不忽略行开头的换行符。只要 getline 遇到换行符,即便它是输入的第一个字符,getline 也将停止读入并返回int main() { string line; // read line at time until end-of-file while (getline(cin, line)) cout << line << endl; return 0; }
-
string 对象的操作
s.empty() 如果 s 为空串,则返回 true,否则返回 false。 s.size() 返回 s 中字符的个数 s[n] 返回 s 中位置为 n 的字符,位置从 0 开始计数 s1 + s2 把 s1 和s2 连接成一个新字符串,返回新生成的字符串 s1 = s2 把 s1 内容替换为 s2 的副本 v1 == v2 比较 v1 与 v2 的内容,相等则返回 true,否则返回 false !=, <, <=, >, and >= 保持这些操作符惯有的含义 -
string::size_type 类型
string size() 操作返回的是 string::size_type 类型的值,是 unsigned int型
-
string 关系操作符
string 对象比较操作是区分大小写的,任何一个大写之母都小于任意的小写字母。
比较策略:
• 如果两个 string 对象长度不同,且短的 string 对象与长的 string 对象的前面部分相匹配,则短的 string 对象小于长的 string 对象。
• 如果 string 对象的字符不同,则比较第一个不匹配的字符 -
string 对象的赋值
// st1 is an empty string, st2 is a copy of the literal string st1, st2 = "The expense of spirit"; st1 = st2; // replace st1 by a copy of st2
-
两个string 对象相加
string s1("hello, "); string s2("world\n"); string s3 = s1 + s2; // s3 is hello, world\n
-
和字符串字面值的连接
+ 操作符的左右操作数必须至少有一个是 string 类型的
string s1 = "hello"; // no punctuation string s2 = "world"; string s3 = s1 + ", "; // ok: adding a string and a literal string s4 = "hello" + ", "; // error: no string operand string s5 = s1 + ", " + "world"; // ok: each + has string operand string s6 = "hello" + ", " + s2; // error: can't add string literals
-
从string 对象获取字符
string 类型通过下标操作符([ ])来访问 string 对象中的单个字符。下标操作符需要取一个 size_type 类型的值,来标明要访问字符的位置。
string 对象的下标从 0 开始。如果 s 是一个 string 对象且 s 不空,则 s[0] 就是字符串的第一个字符, s[1] 就表示第二个字符(如果有的话),而 s[s.size() - 1] 则表示 s 的最后一个字符。
string 对象的索引变量最好也用 string::size_type 类型。string str("some string"); for (string::size_type ix = 0; ix != str.size(); ++ix) cout << str[ix] << endl;
-
string find函数
size_type find(const string & str, size_type pos = 0) const 从字符串的pos位置开始,查找子字符串str。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos size_type find(const char * s, size_type pos = 0) const 从字符串的pos位置开始,查找子字符串s。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos size_type find(const char * s, size_type pos = 0, size_type n) const 从字符串的pos位置开始,查找s的前n个字符组成的子字符串。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos size_type find(const char ch, size_type pos = 0) const 从字符串的pos位置开始,查找字符ch。如果找到,则返回该子字符串首次出现的位置;否则,返回string::npos -
字符数组与string的相互转换
1、字符数组转化成string类型
char ch [] = "ABCDEFG"; string str(ch);//也可string str = ch; 或者 char ch [] = "ABCDEFG"; string str; str = ch;//在原有基础上添加可以用str += ch;
2、将string类型转换为字符数组
char buf[10]; string str("ABCDEFG"); length = str.copy(buf, 9); buf[length] = '\0'; 或者 char buf[10]; string str("ABCDEFG"); strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 10);
-
迭代器iterator
string::iterator it; string str = "abcdefghijklmn"; //s.begin() 返回字符串s第一个字符的位置 char a = *(str.begin()); // a //s.end() 返回字符串s最后一个字符串的后一个位置 char b = *(str.end()-1); // n //s.rbegin() 返回字符串s最后一个字符的位置 char c = *(str.rbegin()); // n //s.rend() 返回字符串s第一个字符的前一个位置 char d = *(str.rend()-1); // a for (string::iterator iter = str.begin(); iter != str.end(); iter++) { cout << *iter; // 依次输出"abcdefghijklmn" } ```
-
反向迭代器reverse_iterator
class Solution { public: int lengthOfLastWord(string s) { int cnt =0; string::reverse_iterator it = s.rbegin(); for(it= s.rbegin() ; it != s.rend(); ++it) { if(*it >= 'A' && *it <='z') { ++cnt; if((it+1) != s.rend() && *(it+1) == ' ') { return cnt; } } } return cnt; } };
-
插入
string str = "hello world"; string str2 = "hard "; string str3 = "it is so happy wow"; s.insert(s.it,n,ch) 在字符串s的it指向位置上插入n个字符ch str.insert(str.begin()+6, 4, 'z'); // str = "hello zzzzworld"
-
删除
string str = "welcome to my blog"; //s.erase(s.ita,s.itb) 把当前字符串s的[ita,itb)删除 str.erase(str.begin()+11, str.begin()+14); // str = "welcome to blog"
-
翻转
string str = "abcdefghijklmn"; reverse(str.begin(),str.end()); // str = "nmlkjihgfedcba"
C++ string
于 2022-03-27 18:29:41 首次发布