string类对象的修改操作
// push_back int main() { string str("hello world"); cout << str << endl; str.push_back('!'); cout << str << endl; return 0; }
// append int main() { string str1; string str2("hello"); string str3("world"); // append(const string& str) str1.append(str2); cout << str1 << endl; // append(const string& str, size_t subpos, size_t sublen) str1.append(str1, 2, 2); cout << str1 << endl; // append(const char* s) str1.append("!!!"); cout << str1 << endl; // append(const char* s, size_t n) str1.append("world", 4); cout << str1 << endl; // append(size_t n, char c) str1.append(3, '?'); cout << str1 << endl; return 0; }
// operator+= int main() { string str1("hello"); string str2("world"); str1 += " c++ "; str1 += str2; str1 += '!'; cout << str1 << endl; return 0; }
// insert() int main() { string str1 = "hello "; string str2 = "world"; string str3 = "c/c++"; string::iterator it; // string& insert(size_t pos, const string& str) str1.insert(6, str2); cout << str1 << endl; // string& inser(size_t pos, const string& str, size_t subpos, size_t sublen) str1.insert(6, str3, 2, 3); cout << str1 << endl; // string& insert(size_t pos, const char* s) str1.insert(5, " vue"); cout << str1 << endl; // string& insert(size_t pos, const char* s, size_t n) str1.insert(5, " javascipt", 5); cout << str1 << endl; // string& insert(size_t pos, size_t n, char c) str1.insert(0, 3, '!'); cout << str1 << endl; // void insert(iterator p, size_t n, char c) str1.insert(str1.end(), 3, '!'); cout << str1 << endl; // iterator insert(iterator p, char c) it = str1.insert(str1.begin() + 3, ' '); cout << str1 << endl; return 0; }
// replace int main() { string str1("in me the tiger "); string str2("sniffs the rose"); string str3("hello world"); string str4("love"); // string& replace(size_t pos, size_t len, const string& str) str1.replace(10, 6, str2); cout << str1 << endl; // string& replace(iterator i1, iterator i2, const string& str) str1.replace(str1.begin(), str1.end() - 5, str3); cout << str1 << endl; // string& replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) str1.replace(6, 5, str2, 0, 6); cout << str1 << endl; // string& replace(size_t pos, size_t len, const char* s) str1.replace(6, 6, "mood"); cout << str1 << endl; // string& replace(iterator i1, iterator i2, const char* s) str1.replace(str1.begin(), str1.begin() + 5, "iterator"); cout << str1 << endl; // string& replace(size_t pos, size_t len, const char* s, size_t n) str1.replace(2, 6, " good job", 5); cout << str1 << endl; // string& replace(iterator i1, iterator i2, const char* s, size_t n) str1.replace(str1.begin(), str1.begin() + 2, "capacity", 3); cout << str1 << endl; // string& replace(size_t pos, size_t len, size_t n, char c) str1.replace(0, 3, 3, 's'); cout << str1 << endl; // string& replace(iterator i1, iterator i2, size_t n, char c) str1.replace(str1.end() - 4, str1.end(), 3, '!'); cout << str1 << endl; return 0; }
// swap int main() { string str1("hello"); string str2("world"); cout << str1 << " " << str2 << endl; printf("%p %p\n", &str1, &str2); str1.swap(str2); cout << str1 << " " << str2 << endl; printf("%p %p\n", &str1, &str2); return 0; }
// c_str int main() { string s1("hello world"); cout << s1 << endl; cout << s1.c_str() << endl; cout << (void*)s1.c_str() << endl; cout << s1 << endl; cout << s1.c_str() << endl; s1 += '\0'; s1 += '\0'; s1 += "xxxxx"; cout << s1 << endl; cout << s1.c_str() << endl; return 0; }
// find int main() { string str1("in me the tiger sniffs the rose"); string str2("tiger"); // size_t find(const string& str, size_t pos = 0) const; size_t found = str1.find(str2); if (found != string::npos) cout << found << endl; // size_t find(const char* s, size_t pos = 0) const; found = str1.find("sniffs"); if (found != string::npos) cout << found << endl; // size_t find(const char* s, size_t pos, size_t n) const; found = str1.find("the tiger", 8, 3); if (found != string::npos) cout << found << endl; // size_t find(char c, size_t pos = 0) const; found = str1.find('t'); if (found != string::npos) cout << found << endl; return 0; }
// substr int main() { string str1("in me the tiger sniffs the rose"); string str2 = str1.substr(3, 2); cout << str2 << endl; size_t pos = str1.find("tiger"); string str3 = str1.substr(pos); cout << str3 << endl; return 0; }
string类非成员函数
// operator+ int main() { string firstlevel("com"); string secondlevel("cplusplus"); string scheme("http://"); string hostname; string url; hostname = " www." + secondlevel + '.' + firstlevel; url = scheme + hostname; cout << url << endl; return 0; }
// getline int main() { string name; getline(cin, name); cout << "Hello, " << name << endl; return 0; }
// relational operators int main() { string str1("alpha"); string str2("beta"); if (str1 == str2) cout << "equal" << endl; if (str1 != str2) cout << "not equal" << endl; if (str1 < str2) cout << "less" << endl; if (str1 > str2) cout << "greater" << endl; if (str1 <= str2) cout << "less or equal" << endl; if (str1 >= str2) cout << "greater or equal" << endl; return 0; }