string 是 C++ 对数据结构字符动态数组的实现。要使用 string,请在程序头添加:数组
#include <string> using name space std;
string<int> s;
定义一个空 string 对象string<int> s(3,'a');
s = "aaa"string<int> s("aaa");
string<int> s1(s);
s1 = sstring<int> s1 = s;
string<int> s { "abc" };
s = "abc"string<int> s = { "abc" };
string<int> s = "abc";
数据结构
string 能够看作是一种专门用于存储字符的 vector,所以与 vector 大部分的操做也相似。函数
string s ("abc"); s.push_back('d'); // s = "abcd";
插入单个元素spa
string s ("abc"); s.insert(s.begin(), 'd'); // s = "dabc";
插入一段元素code
string s ("ab"); string s1 ("cd"); s1.insert(s1.begin(), s.begin(), s.end()); // s1 = "abcd";
在循环中插入元素
插入元素一般会使迭代器失效,这会给在循环中插入元素带来不小的麻烦。insert 操做在插入元素成功后会返回插入位置的有效迭代器。对象
string s ("ab"); string s1 ("cd"); auto it = s1.begin(); for (auto c:s) auto it = s1.insert(it, c); // s1 = "bacd";
string s ("abc"); s.pop_back(); // s = "ab";
删除一个元素内存
string s ("abc"); s.erase(s.begin()); // s = "bc";
删除一段元素ci
string s ("abc"); s.erase(s.begin(), s.begin()+2); // s = "c";
在循环中删除元素
删除元素一般会使迭代器失效,这会给在循环中删除元素带来不小的麻烦。erase 操做在插入元素成功后会返回插入位置的有效迭代器。字符串
string s ("abcd"); auto it = s.beign(); while (it!=s.end()) { //删除 c 以前的字母 if (*it < 'c') auto it = s.erase(it); else it++; } // s = "c";
像数组同样,string 支持下标随机访问get
string s ("abcd"); cout << s[1]; // 输出 b
string s ("abcd"); //将 s 中在 c 前面的字母置为 c for (auto it=s.begin(); it!=s.end(); it++) { if (*it < c) *it = c; } // s = "cccd"
string s ("abcd"); //输出 s 中在 c 以前的字母 for (auto e : s) { if (e < 'c') cout << e; } // 输出:ab
注意:这种方式获得的 e 是 s 中元素的拷贝,若想要获得 s 元素的自己,请使用 &for (auto &e:s)
string 类提供了 6 个不一样的搜索函数。每一个搜索函数都返回一个 string :: size_type 类型的值,表示匹配发生位置的下标。若是搜索失败,则返回一个名为 string :: npos 的 static 成员 (string :: size_type npos = -1,因为 string :: size_type 实际上就是 unsigned int 类型,故 npos == UINT_MAX)
s.find(c)
、s.find(s1)
在 s 搜索指定的字符 c 或者字符串 s1,返回第一个匹配位置的下标。
string s {"hello"}; cout << s.find('l'); // 输出 2 cout << s.find("he");// 输出 0 cout << s.find("eo");// 输出 UINT_MAX
s.rfind(c)
、s.rfind(s1)
在 s 中搜索指定的字符 c 或者字符串 s1,返回最后一个匹配位置的下标。
s.find_first_of(c)
、s.find_first_of(s1)
在 s 中搜索指定的字符 c 或者字符串 s1 中的任何一个字符,返回第一个匹配位置的下标。
string s {"hello"}; cout << s.find_first_of('l'); // 输出 2 cout << s.find_first_of("eo");// 输出 1
s.find_last_of(c)
、s.find_last_of(s1)
在 s 中搜索指定的字符 c 或者字符串 s1 中的任何一个字符,返回最后一个匹配位置的下标。
s.find_first_not_of(c)
、s.find_first_not_of(s1)
在 s 中搜索不是字符 c 或者不在字符串 s1 中的字符,返回第一个匹配位置的下标。
string s {"hello"}; cout << s.find_first_not_of('h'); // 输出 1 cout << s.find_first_not_of("heo");// 输出 2
s.find_last_not_of(c)
、s.find_last_not_of(s1)
在 s 中搜索不是字符 c 或者不在字符串 s1 中的字符,返回最后一个匹配位置的下标。
上述 string 的 6 个搜索函数都有一个带有指定开始加搜索位置的参数的重载版本。如:s.find(c, string::size_type pos)
sstream 头文件定义了三个类型来支持内存 IO。
istringstream 从 string 读取数据,ostringstream 向 string 写入数据,而 stringstream 既能够从 string 读取数据,也能够向 string 写入数据。要使用这些类型,须要包涵头文件 include<sstream>
using namespace std;
istringstream is;
建立一个输入流 ostringstream os;
stringstream ss;
istringstream is(s);
建立一个输入流并绑定 string s ostringstream os(s);
stringstream ss(s);
is.str();
返回 is 绑定的 string os.str();
ss.str();
is.str(s);
将 string s 拷贝(绑定)到 is 中 os.str(s);
ss.str(s);
在进行处理文本时,若是文本中的元素类型混杂(例如既有数字也有单词),亦或元素的数目不定。使用 istringstream 将带来很大的方便。
例如,输入算式的值,须要咱们计算这个算式的值。一个必要的预处理即是将输入字符串中的数字和运算符号解析出来。使用 istreamstring 能够很轻松的完成这一工做
string str; cin >> str; str.push_back('$'); // 为方便处理,添加一个结束标记符 $ istringstream is(str); vector<int> nums; vector<char> syms; int n; char c; while (is >> n) { nums.push_back(n); is >> c; syms.push_back(c); } // 输入:2*3 - 5/4 + 6 // nums = { 2, 3, 5, 4, 6 }; syms = { '*', '-', '/', '+', '$' };
有时,咱们输出的内容中包含多种多样的元素类型,此时可先将一些输出写入输出流,带输出流填充完毕后,再一次性地输出。
例如,假设咱们要读取一行以空格分开的数字,并将分隔符由空格变为等号输出。
string str; getline(cin, str); // 输入:1 2 3 4 5 istringstream is(str); ostringstream os; int n; is >> n; // 为了避免在最末尾添加‘,’,进行了一下调整 os << n; while (is >> n) { os << ','; os << n; } cout << os.str(); // 输出:1,2,3,4,5