1. C++中的標準模板庫中的string可以讓我們很方便地對字符串進行操作,對基本的字符串的需求進行了一定的封裝,如果需要使用到string那麼需要引入string的頭文件,即#include<string>還需要在頭文件下加上using namespace std;,下面是string類型的常見函數以及用法
2. ① string中內容的訪問,有兩種方式,一種是通過下表來進行訪問,另外一種是通過迭代器來進行訪問
有些函數比如像insert與erase函數必須通過迭代器作爲參數進行傳入,string::iterator it表示定義一個迭代器,我們可以通過*it來訪問string中的每一位
下面是具體的程序:
#include<iostream>
#include<string>
using namespace std;
int main(void){
//通過下標來進行訪問
string str = "abcd";
for(int i = 0; i < str.length(); ++i){
cout << str[i];
}
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(void){
//通過迭代器來進行訪問
string str = "abcd";
//對於inset和erase函數來說需要傳入的參數是迭代器
for(string :: iterator it = str.begin(); it != str.end(); it++){
cout << *it;
}
return 0;
}
② 兩個string類型的比較可以直接通過<、>、<=、>=、!=、==來進行判斷,比較的規則是字典序:
#include<iostream>
#include<string>
using namespace std;
int main(void){
string str1 = "aa", str2 = "aaa", str3 = "abc", str4 = "xyz";
//可以直接使用==/!=/>=/<=等運算符來進行比較
if(str1 < str2){
cout << "ok1" << endl;
}
if(str1 != str3){
cout << "ok2" << endl;
}
if(str4 >= str3){
cout << "ok3" << endl;
}
return 0;
}
③ insert函數,有一下集中用法
1)insert(pos,string)在pos號位置插入字符串string
2)insert(it,it1,it2),it爲原字符串插入的位置,it2和it3爲待插入字符串的首位迭代器,用來表示串[it2,it3)插入到it位置上
下面是具體的程序:
#include<iostream>
#include<string>
using namespace std;
int main(void){
//insert(pos, string)在pos位置插入字符串string
string str = "abcdxyz", str2 = "opq";
str.insert(3, str2);
cout << str << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(void){
//insert(pos, string)在pos位置插入字符串string
string str = "abcdxyz", str2 = "opq";
str.insert(str.begin() + 2, str2.begin(), str2.end());
cout << str << endl;
return 0;
}
④ erase函數,有兩種用法,一種是刪除單個元素,另外一種是刪除一個區間內的所有函數
1)刪除單個元素
str.erase(it)爲刪除單個元素,it爲需要刪除的元素的迭代器
2)刪除一個區間內的所有元素
str.earse(first,last)其中first爲需要刪除區間的起始迭代器,而last則爲需要刪除的區間的末尾迭代器的下一個地址,也即刪除[first,last)
3)str.earse(pos,length)其中pos爲需要開始刪除的起始位置,length爲刪除的字符的個數
具體代碼如下:
#include<iostream>
#include<string>
using namespace std;
int main(void){
string str = "abcdefg";
//刪除字符串的單個元素
str.erase(str.begin() + 4);
cout << str << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(void){
string str = "abcdefg";
//刪除字符串的一個區間的所有元素
str.erase(str.begin() + 2, str.end() - 1);
cout << str << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main(void){
//第一個參數爲刪除的起始位置,第二個參數是需要刪除的字符的個數
string str = "abcdefg";
str.erase(3, 2);
cout << str << endl;
return 0;
}
⑤ clear函數,用來清空string中的數據,代碼如下:
#include<iostream>
#include<string>
using namespace std;
int main(void){
string str = "abcdefg";
str.clear();
cout << str.length() << endl;
return 0;
}
⑥ substr函數,substr(pos,len)返回從pos號位開始,長度爲len的子串,具體代碼如下:
#include<iostream>
#include<string>
using namespace std;
int main(void){
//substr截取字符串,第一個參數是從pos位置開始第二個參數是截取的長度
string str = "Thank you for your smile.";
cout << str.substr(0, 5) << endl;
cout << str.substr(14, 4) << endl;
cout << str.substr(19, 5) << endl;
return 0;
}
⑦ find函數,str.find(str2),當str2是str的子串的時候,返回其在str中第一次出現的位置,如果str2不是str的子串那麼返回string:npos
str.find(str2, pos)從str的pos號位開始匹配str2,返回值與上面的相同,下面是具體的代碼:
#include<iostream>
#include<string>
using namespace std;
int main(void){
string str = "Thank you for your smile.";
string str2 = "you";
string str3 = "me";
//返回str2在str中第一次出現的位置
if(str.find(str2) != string::npos) cout << str.find(str2) << endl;
if(str.find(str2, 7) != string::npos) cout << str.find(str2, 7) << endl;
if(str.find(str3) != string::npos) cout << str.find(str3) << endl;
return 0;
}