string s,表示s是一個string類的對象,有自己的成員變量和成員函數
為了和上一篇的結尾呼應,先說明string類對象的sizeof的結果:
string s = “ahdskahlal”;
sizeof(s) = 32(x86)或者64(x64);
下面介紹和string類相關的函數
http://www.cppblog.com/lmlf001/archive/2006/04/19/5883.html
這篇文章介紹的很全,下面貼一些代碼加深印象(其實這是百度的總結在一起了)
#include <iostream> #include <string> #include <sstream>
using namespace std; int main() { //1.string類重載運算符operator>>用於輸入,同樣重載運算符operator<<用於輸出操作
string str1; getline(cin, str1);//字符串的行輸入
cout << str1 << endl; cout<<endl; //2.string類的構造函數
char *s = "bbbbb"; string str2(s);//用c字符串s初始化
cout << str2 << endl; char ch = 'c'; string str3(5, ch);//用n個字符ch初始化
cout << str3 << endl; cout<<endl; //3.string類的字符操作
string str4 = "abcde"; ch = str4.at(4);//at()返回當前字符串中第n個字符的位置,並且提供范圍檢查,當越界時會拋出異常!
cout << ch << endl; cout<<endl; //4.string的特性描述
string str5 = "abcdefgh"; int size; size = str5.capacity();//返回當前容量,一般在x86上都是輸出15
cout << size << endl; size = str5.max_size();//返回string對象中可存放的最大字符串的長度 ,一般在x86上都是輸出-2
cout << size << endl; size = str5.size();//返回當前字符串的大小
cout << size << endl; size = str5.length();//返回當前字符串的長度
cout << size << endl; int len = 10; str5.resize(len, ch);//把字符串當前大小置為len,並用字符ch填充不足的部分
cout << str5 << endl; cout<<endl; //5.string的賦值
string str6; str6.assign(str5);//把字符串str5賦給當前字符串
cout << str6 << endl; str6.assign(s);//用c類型字符串s賦值
cout << str6 << endl; str6.assign(s, 2);//用c類型字符串s開始的n個字符賦值
cout << str6 << endl; str6.assign(len, ch);//用len個字符ch賦值給當前字符串
cout << str6 << endl; str6.assign(str5, 0, 3);//把字符串str7中從0開始的3個字符賦給當前字符串
cout << str6 << endl; cout<<endl; //6.string的連接,用append
string com; com += str6;//把字符串str9連接到當前字符串的結尾
cout << com << endl; com.append(s);//把c類型字符串s連接到當前字符串的結尾
cout << com << endl; com.append(s, 2);//把c類型字符串s的前2個字符連接到當前字符串的結尾
cout << com << endl; com.push_back('k');//把一個字符連接到當前字符串的結尾
cout << com << endl; cout<<endl; //7.string的比較 ,用compare //以下的3個函數同樣適用於c類型的字符串,在compare函數中>時返回1,<時返回-1,=時返回0
int flag; flag = com.compare(str6);//比較兩個字符串的大小,通過ASCII的相減得出!
cout << flag << endl; flag = com.compare(6, 12, str6);//比較com字符串從6開始的12個字符組成的字符串與str6的大小
cout << flag << endl; flag = com.compare(6, 12, str6, 3, 5);//比較com字符串從6開始的12個字符組成的字符串與str6字符串從3開始的5個字符組成的字符串的大小
cout << flag << endl; cout<<endl; //8.string的子串
string str7; str7= com.substr(10, 15);//返回從下標10開始的15個字符組成的字符串
cout << str7 << endl; cout<<endl; //9.string的交換
str7.swap(com);//交換str7與com的值
cout << str7 << endl; cout<<endl; //10.string的查找,查找成功時返回所在位置,失敗時返回string::npos的值,即是-1
string str8 = "abcdefghijklmnopqrstuvwxyz"; int pos; pos = str8.find('i', 0);//從位置0開始查找字符i在當前字符串的位置
cout << pos << endl; pos = str8.find("ghijk", 0);//從位置0開始查找字符串“ghijk”在當前字符串的位置
cout << pos << endl; pos = str8.find("opqrstuvw", 0, 4);//從位置0開始查找字符串“opqrstuvw”前4個字符組成的字符串在當前字符串中的位置
cout << pos << endl; pos = str8.rfind('s', string::npos);//從字符串str8反向開始查找字符s在字符串中的位置
cout << pos << endl; pos = str8.rfind("klmn", string::npos);//從字符串str8反向開始查找字符串“klmn”在字符串中的位置
cout << pos << endl; pos = str8.rfind("opqrstuvw", string::npos, 3);//從string::pos開始從后向前查找字符串s中前n個字符組成的字符串在當前串中的位置
cout << pos << endl; cout<<endl; string str9 = "aaabbbcccddeeffgghhiijjkkllmmandjfaklsdfpopdtwptioczx"; pos = str9.find_first_of('d', 0);//從位置0開始查找字符d在當前字符串第一次出現的位置
cout << pos << endl; pos = str9.find_first_of("eefff", 0);//從位置0開始查找字符串“eeefff“在當前字符串中第一次出現的位置
cout << pos << endl; pos = str9.find_first_of("efff", 0, 3);//從位置0開始查找當前串中第一個在字符串”efff“的前3個字符組成的數組里的字符的位置
cout << pos << endl; pos = str9.find_first_not_of('b', 0);//從當前串中查找第一個不在串s中的字符出現的位置
cout << pos << endl; pos = str9.find_first_not_of("abcdefghij", 0);//從當前串中查找第一個不在串s中的字符出現的位置
cout << pos << endl; pos = str9.find_first_not_of("abcdefghij", 0, 3);//從當前串中查找第一個不在由字符串”abcdefghij”的前3個字符所組成的字符串中的字符出現的位置
cout << pos << endl; //下面的last的格式和first的一致,只是它從后面檢索!
pos = str9.find_last_of('b', string::npos); cout << pos << endl; pos = str9.find_last_of("abcdef", string::npos); cout << pos << endl; pos = str9.find_last_of("abcdef", string::npos, 2); cout << pos << endl; pos = str9.find_last_not_of('a', string::npos); cout << pos << endl; pos = str9.find_last_not_of("abcdef", string::npos); cout << pos << endl; pos = str9.find_last_not_of("abcdef", string::npos, 3); cout << pos << endl; cout<<endl; //11.string的替換,用replace
string str10 = "abcdefghijklmn"; str10.replace(0, 3, "qqqq");//刪除從0開始的3個字符,然后在0處插入字符串“qqqq”
cout << str10 << endl; str10.replace(0, 3, "vvvv", 2);//刪除從0開始的3個字符,然后在0處插入字符串“vvvv”的前2個字符
cout << str10 << endl; str10.replace(0, 3, "opqrstuvw", 2, 4);//刪除從0開始的3個字符,然后在0處插入字符串“opqrstuvw”從位置2開始的4個字符
cout << str10 << endl; str10.replace(0, 3, 8, 'c');//刪除從0開始的3個字符,然后在0處插入8個字符 c
cout << str10 << endl; cout<<endl; //12.string的插入
string str11 = "abcdefg"; str11.insert(0, "mnop");//在字符串的0位置開始處,插入字符串“mnop”
cout << str11 << endl; str11.insert(0, 2, 'm');//在字符串的0位置開始處,插入2個字符m
cout << str11 << endl; str11.insert(0, "uvwxy", 3);//在字符串的0位置開始處,插入字符串“uvwxy”中的前3個字符
cout << str11 << endl; str11.insert(0, "uvwxy", 1, 2);//在字符串的0位置開始處,插入從字符串“uvwxy”的1位置開始的2個字符
cout << str11 << endl; cout<<endl; //13.string的刪除
string str12 = "gfedcba"; string::iterator it; it = str12.begin(); it++; str12.erase(it);//刪除it指向的字符,返回刪除后迭代器的位置
cout << str12 << endl; str12.erase(it, it+3);//刪除it和it+3之間的所有字符,返回刪除后迭代器的位置
cout << str12 << endl; str12.erase(2);//刪除從字符串位置3以后的所有字符,返回位置3前面的字符
cout << str12 << endl; cout<<endl; //14.字符串的流處理
string str13("hello,this is a test"); istringstream is(str13); string s1,s2,s3,s4; is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
ostringstream os; os<<s1<<s2<<s3<<s4; cout<<os.str() << endl; system("pause"); }
兩外還有三個和string相關的成員函數,從一個string得到c類型的字符數組:c_str()、data()、copy(p,n)。
c_str()\data():生成一個const char*指針,指向以\不以空字符終止的數組。
這個數組的數據是臨時的,當有一個改變這些數據的成員函數被調用后,其中的數據就會失效,因此要么現用現轉換,要么復制到用戶自己可以管理的內存中,運用strcpy()函數來復制。比如下面的例子(換成c = s.data()也一樣):
#include<iostream> #include<string>
using namespace std; int main() { const char* c; string s="1234"; c = s.c_str(); cout<<c<<endl; //輸出:1234 //將c復制到用戶管理的內存myself中
char * myself = new char[10]; strcpy(myself,c); s="abcd"; cout<<c<<endl; //輸出:abcd
cout<<myself<<endl; //輸出:1234
}
copy(p,n,size_type _Off = 0):從string類型對象中至多復制n個字符到字符指針p指向的空間中。默認從首字符開始,但是也可以指定,開始的位置(記住從0開始)。返回真正從對象中復制的字符。------用戶要確保p指向的空間足夠保存n個字符。
#include<iostream> #include<string>
using namespace std; int main() { string s="1234abdd"; char dest[20] = {'\0'}; int n = s.copy(dest,10); cout<<n<<endl; //輸出:8
cout<<dest<<endl; //輸出:1234abdd
string ss = "xyz"; n = ss.copy(dest,2,1); cout<<n<<endl; //輸出:2
cout<<dest<<endl; //輸出:yz34abdd
}