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