標准庫中的字符串類
C++語言直接支持C語言所有概念。
C++中沒有原生的字符串類型。
由於C++中沒有原生的字符串類型,C++標准庫提供了string類型。
1、string 直接支持字符串鏈接
2、字符串大小比較
/*實驗 字符串排序 拼接*/
#include <iostream> #include <string> #include <sstream> using namespace std; /*實驗1 字符串排序 拼接*/ string string_add(string s[],int addnumber) { string ret=""; for(int i= 0; i<addnumber ;i++) { ret += s[i]+';';//字符串拼接 } return ret; } //首字母排序 swap();C++交換函數 //按照首字母排序 void string_sord(string s[],int len) { for(int i = 0;i<len ; i++) { for(int j=i ;j<len;j++) if(s[i] > s[j]) swap(s[i],s[j]);//使用 swap()交換兩個字符串 } } int main() { string sTest[3] = { "abc", "cba", "bwe" }; cout<<string_add(sTest,3)<<endl; cout<<endl; string_sord(sTest,3); for(int i = 0; i< 3;i++) cout<< sTest[i]<<endl; return 0; }
輸出結果:
abc;cba;bwe;
abc
bwe
cba
使用C++標准庫中的string 進行字符串的拼接 排序。
3、子串查找和提取
4、字符串的插入和替換
字符串與數字的轉換:
1、標准庫中提供了相關類對字符串和數字進行轉換。
2、字符串sstream 用於string 的轉換
2.1、<sstream> 頭文件
2.2、istringstream 字符串輸入流
2.3、ostringstream 字符串輸出流
字符串轉數字、數字轉字符串。
istringstream與ostringstream 字符串流使用
#include <iostream> #include <string> #include <sstream> using namespace std; //string to float void stringToNumber(const string s,float& n) { istringstream oss(s);//創建istringstream流 將字符傳入istringstream流中 oss>>n;//將字符s 轉換為數字n //istringstream(s)>>n;//使用istringstream 字符串輸入流 } //數字 轉 字符 void numberToString(const float& f, string& s) { ostringstream oss;//創建 ostringstream 流 oss << f;//將數字傳入流 s=oss.str();//使用流中的.str()函數獲取字符串 //s= ((ostringstream&)(ostringstream()<<f)).str(); } //由於實現的是float之間的相互轉換。要實現int 還需要寫,下面通過宏定義的方式進行修改,后期使用模塊函數修改。 //使用臨時變量 ,由於臨時變量只有一條語句的聲明周期。 //修改如下 #define toNumber(s,n) (istringstream(s)>>n) #define toString(n) (((ostringstream&)(ostringstream()<<n)).str()) int main() { float temp_val; stringToNumber("1.12",temp_val); cout << temp_val <<endl; string sTest = ""; numberToString(1.22,sTest); cout <<sTest <<endl; double val=0; if(toNumber("123",val)) cout << val << endl; cout << toString(13.25) <<endl; return 0; }
運行結果:
1.12 1.22 123 13.25
字符串左右移動:(實現字符串的 << >>操作) 實現字符串的循環左右移動。
#include <iostream> #include <string> #include <sstream> using namespace std; //使用字符串 string_nam.substr();//字符串剪切函數 //string_nam.length(); 獲取當前字符串長度 void testFun() { string s = "abcdef"; string sOut =""; sOut = s.substr(3); sOut += s.substr(0,3); cout <<sOut <<endl; cout << s.length() <<endl; } void testFun1( string &s,int n) { string temp =""; unsigned int mark = 0; n = n %s.length();//防止n 超過字符串的長度。移動長度與字符串長度相等相當於不移動 mark = s.length()-n;//獲取字符串長度 temp = s.substr(mark);//從 第mark個數字截取后續的字符 temp += s.substr(0,mark);//截取從0到mark的字符 ,完成字符移動 s = temp; } string operator >> (const string &s ,int n ) { string temp =""; unsigned int mark = 0; n = n %s.length();//防止n 超過字符串的長度。移動長度與字符串長度相等相當於不移動 mark = s.length()-n;//獲取字符串長度 temp = s.substr(mark);//從 第mark個數字截取后續的字符 temp += s.substr(0,mark);//截取從0到mark的字符 ,完成字符移動 return temp; } string operator << (const string &s ,int n ) { string temp =""; unsigned int mark = 0; n = n %s.length();//防止n 超過字符串的長度。移動長度與字符串長度相等相當於不移動 mark = s.length()-n;//獲取字符串長度 temp = s.substr(s.length()-mark);//從 第mark個數字截取后續的字符 temp += s.substr(0,s.length()-mark);//截取從0到mark的字符 ,完成字符移動 return temp; } int main() { string sTest ="abcdefghij"; cout << sTest <<endl; string sTest2 =sTest>>3; cout << sTest2 <<endl; sTest2 = sTest<<1; cout << sTest2 <<endl; return 0; }
運行:
abcdefghij
hijabcdefg
bcdefghija