string
string类可用“=”赋值,用“==”等值比较,用加号做串联。
包含头文件<string>
string类支持cin方式和cout方式输入输出。
#include <iostream> #include <string> using namespace std; int main() { string stuff; cin >> stuff; cout << stuff; return 0; }
string类构造函数
string() | 创建空的字符串 |
string(const char *s) | 用C字符串创建string对象 |
string(const string &s) | 用现有字符串创建新的字符串 |
#include <iostream> #include <string> using namespace std; int main() { string stuff("hello"); string stuff1 = stuff; cout << stuff1 << endl; return 0; }
string重载运算符
>> | << | = | += | + | [] | 关系运算符 |
#include <iostream> #include <string> using namespace std; int main() { string s1("hello"); string s2 = "beijing"; if (s1 == s2) cout << s1[1]; else cout << s2 + "china"; return 0; }
string常用方法
#include <iostream> #include <string> using namespace std; int main() { string s1("aaaaaaaa"); string s2("beijing"); s1.append(s2); s1.append(s2, 2, 3); s1.append(2, 'A'); s1.assign(s2); s1.assign(s2, 2, 3); cout << s2.at(2) << endl; cout << *s2.begin() << endl; cout << s2.capacity() << endl; cout << s2.size() << endl; cout << s1.compare(s2) << endl; cout << s2.compare(s1) << endl; cout << s2.empty() << endl; cout << *(s2.end() - 1) << endl; cout << s2.erase(2, 3) << endl; cout << s2 << endl; string s3 = "eij"; cout << s2.find(s3) << endl; cout << s2.find('b') << endl; cout << s1.insert(2, "bb") << endl; cout << s2.substr(2, 3) << endl; s1.swap(s2); cout << s1; return 0; }
字符串流的类型
sstream 基于 std::string 编写的,包含:
- class istringstream;
- class ostringstream;
- class stringbuf;
- class stringstream;
ostringstream::str() 返回 std::string 类型的字符串
strstream 基于C类型字符串char*编写的,包含:
- class strstreambuf;
- class istrstream;
- class ostrstream;
- class strstream;
ostrstream::str() 返回 char* 类型的字符串
#include <iostream> #include <sstream> using namespace std; int main() { // istringstream istr; // istr.str("1 56.7"); istringstream istr("1 56.7"); // 空格为字符串的内部分界 cout << istr.str() << endl; int a; float b; istr >> a; cout << a << endl; istr >> b; cout << b << endl; return 0; }
put()
#include <iostream> #include <sstream> using namespace std; int main() { ostringstream ostr; ostr.put('d'); ostr.put('e'); ostr << "fg"; string gstr = ostr.str(); cout << gstr; return 0; }
put() 或者左移操作符向ostr插入单个字符或字符串
str()
#include <iostream> #include <sstream> using namespace std; int main() { ostringstream outstr; int price = 130; char* ps = " to C++"; outstr << "Pay only$" << price << ps << endl; string msg = outstr.str(); cout << msg << endl; return 0; }
str() 成员函数将stringstream对象转换为string字符串
stringstream
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { stringstream stream; string result; int i = 1000; stream << i; stream >> result; cout << result << endl; return 0; }
stringstream 通常是用来做数据转换的
clear()
#include <iostream> #include <sstream> using namespace std; int main() { stringstream sstr; // int 转 string int a = 100; string str; sstr << a; sstr >> str; cout << str << endl; // string 转 char[] sstr.clear(); string name = "beijing"; char cname[200]; sstr << name; sstr >> cname; cout << cname; return 0; }
多次转换时须调用 stringstream 的成员函数 clear()
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string s = "ab,cd,e,fg,h"; int n = s.size(); for (int i = 0; i < n; ++i) { if (s[i] == ',') s[i] = ' '; } istringstream out(s); string str; out >> str; // 以空格作为字符串的内部分隔 cout << str << endl; while (out >> str) { // 读到文件末尾的时候会返回0 cout << str << ' '; } cout << endl; return 0; }