C++【string】用法和例子

糖醋麻辣蝦發表於2018-08-08
  1 /***
  2  * string 基礎api複習
  3  * 8 AUG 2018
  4  */
  5 
  6 #include <iostream>
  7 #include <string>
  8 using namespace std;
  9 
 10 void print(string str, int index)
 11 {
 12     std::cout << "str" << index << " = " << str << std::endl;
 13 }
 14 
 15 int main()
 16 {
 17     // string物件初始化
 18     string str0("i am a boy~");
 19     string str1 = "hello world!";
 20     string str2(str1);
 21     string str3(5, 'c');
 22     string str4(str1, 2, 3); // 從【2】索引起始,長度為3的字串
 23 
 24     print(str0, 0);
 25     print(str1, 1);
 26     print(str2, 2);
 27     print(str3, 3);
 28     print(str4, 4);
 29 
 30     // string相關簡單函式
 31     bool power;
 32     int str_len;
 33     char te_ch;
 34     string te_str;
 35     power = str0.empty();   // 判斷字串是否為空
 36     std::cout << "power = " << power << std::endl;
 37     str_len = str1.size();  // 字串長度
 38     std::cout << "str_len = " << str_len << std::endl;
 39     te_ch = str2[4];        // 返回字串的第n個字元,下標從0開始
 40     std::cout << "str2[4] = " << te_ch << std::endl;
 41     te_ch = str2.at(4);     // 返回字串的第n個字元,下標從0開始,同上
 42     std::cout << "str2.at(4) = " << te_ch << std::endl;
 43     te_str = str1 + str0;   // 連線兩個字串變成一個新的字串
 44     std::cout << "str1 + str0 = " << te_str << std::endl;
 45     te_str = str3;          // 替換字串
 46     std::cout << "te_str = " << te_str << std::endl;
 47     te_str.append(str0);    // 追加字串
 48     std::cout << "te_str = " << te_str << std::endl;
 49     te_str.assign(str0);    // 為字串物件賦值
 50     std::cout << "te_str = " << te_str << std::endl;
 51 
 52     // str.begin()&&str.end()均為迭代器
 53     string::iterator it = str0.begin();    // 字串迭代器,類似指標
 54     std::cout << "it = " << (*it) << std::endl;
 55 
 56     // string::c_str()
 57     char* char_str = new char[strlen(str0.c_str()) + 1];
 58     strcpy(char_str, str0.c_str());        // 當需要將字串轉成const char*時,用c_str(意味著轉換成C語音的方式)
 59     std::cout << "char_str = " << char_str << std::endl;
 60     delete []char_str;
 61     char_str = NULL;
 62 
 63     // string::erase(),注意,erase後迭代器自動指向後一個位置
 64     str0.erase(5, 2);                                // 從index值為5.刪除長度為4個字元
 65     std::cout << "str0.erase(5, 2) = " << str0 << std::endl;
 66     str0.erase(str0.begin() + 2);                    // 刪除index值為2的字元      
 67     std::cout << "str0.erase(str0.begin() + 2) = " << str0 << std::endl;
 68     str0.erase(str0.begin() + 2, str0.end() - 1);   // 從index值為2,刪除到index末尾減一
 69     std::cout << "str0.erase(str0.begin() + 2) = " << str0 << std::endl;
 70 
 71     // string::find()
 72     std::size_t found = str1.find(str4, 0);         // 從str1中找str4第一次出現的地方
 73     if (found != string::npos)
 74         std::cout << "first found is " << found << std::endl;
 75     found = str1.find('o');                         // 從str1中找o第一次出現的地方
 76     if (found != string::npos)
 77         std::cout << "first found is " << found << std::endl;
 78     found = str1.find("world");                     // 從str1中找world第一次出現的地方
 79     if (found != string::npos)
 80         std::cout << "first found is " << found << std::endl;    
 81 
 82     // string::insert()
 83     str0.insert(1, " love");        // 在str0的index值為1後插入 love
 84     print(str0, 0);
 85     str0.insert(6, " you", 0, 3);   // 在str0的index值為6後插入 you中從index值為0到長度為3的字串
 86     print(str0, 0);
 87     str0.insert(0, str1);           // 在str0的index值為0後插入str1
 88     print(str0, 0);
 89 
 90     // string::length()與string::size()用法相同,值也一樣
 91     std::cout << "str0.length() = " << str0.length() << " str0.size() = " << str0.size() << std::endl;
 92 
 93     // string::maxsize()返回能夠儲存字串的最大長度
 94     std::cout << "str0.max_size() = " << str0.max_size() << std::endl;
 95 
 96     // string::rbegin()和string::rend()逆向迭代器
 97     for (string::reverse_iterator it = str0.rbegin(); it != str0.rend(); it++)
 98     {
 99         std::cout << (*it);
100     }
101     std::cout << std::endl;
102     print(str0, 0);
103 
104     // 這個是algorithm裡面的,混入是為了對比效果
105     reverse(str0.begin(), str0.end()); 
106     print(str0, 0);
107 
108     // string::replace()用法類似string::insert()
109     str0.replace(5, 3, "ni hao", 2, 4);
110     print(str0, 0);
111 
112     // string::substr()
113     te_str = str0.substr(5, 3);
114     std::cout << "te_str = " << te_str << std::endl;
115 
116     // string::swap()
117     te_str.swap(str0);
118     print(str0, 0);
119     std::cout << "te_str = " << te_str << std::endl;
120 
121     while (true)
122     {
123         getchar();
124     }
125     return 0;
126 }

 

相關文章