【C++】C++string類總結
首先,為了在程序中使用string類型,必須包含頭文件 <string>。如下:
#include <string>
註意這裏不是string.h,string.h是C字符串頭文件。
string類是一個模板類,位於名字空間std中,通常為方便使用還需要增加:
using namespace std;
聲明一個字符串變量很簡單:
string str;
測試代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream>
#include <string>
using namespace std;
int main ( )
{
string str; //定義了一個空字符串str
str = "Hello world" ; // 給str賦值為"Hello world"
char cstr[] = "abcde" ; //定義了一個C字符串
string s1(str); //調用復制構造函數生成s1,s1為str的復制品
cout<<s1<<endl;
string s2(str,6); //將str內,開始於位置6的部分當作s2的初值
cout<<s2<<endl;
string s3(str,6,3); //將str內,開始於6且長度頂多為3的部分作為s3的初值
cout<<s3<<endl;
string s4(cstr); //將C字符串作為s4的初值
cout<<s4<<endl;
string s5(cstr,3); //將C字符串前3個字符作為字符串s5的初值。
cout<<s5<<endl; string s6(5, ‘A‘ ); //生成一個字符串,包含5個‘A‘字符
cout<<s6<<endl;
string s7(str.begin(),str.begin()+5); //區間str.begin()和str.begin()+5內的字符作為初值
cout<<s7<<endl;
return 0;
}
|
程序執行結果為:
Hello world
world
wor
abcde
abc
AAAAA
Hello
二、string的比較等操作
你可以用 ==、>、<、>=、<=、和!=比較字符串,可以用+或者+=操作符連接兩個字符串,並且可以用[]獲取特定的字符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cout << "Please input your name:" <<endl;
cin >> str;
if ( str == "Li" ) // 字符串相等比較
cout << "you are Li!" <<endl;
else if ( str != "Wang" ) // 字符串不等比較
cout << "you are not Wang!" <<endl;
else if ( str < "Li" ) // 字符串小於比較,>、>=、<=類似
cout << "your name should be ahead of Li" <<endl;
else
cout << "your name should be after of Li" <<endl;
str += ", Welcome!" ; // 字符串+=
cout << str<<endl;
for ( int i = 0 ; i < str.size(); i ++)
cout<<str[i]; // 類似數組,通過[]獲取特定的字符
return 0;
}
|
程序執行結果為:
Please input your name:
Zhang↙
you are not Wang!
Zhang, Welcome!
Zhang, Welcome!
上例中,“ cout<< str[i]; ”可換為: cout<< str.at(i);
三、string特性描述
可用下列函數來獲得string的一些特性:
int capacity()const; //返回當前容量(即string中不必增加內存即可存放的元素個數)
int max_size()const; //返回string對象中可存放的最大字符串的長度
int size()const; //返回當前字符串的大小
int length()const; //返回當前字符串的長度
bool empty()const; //當前字符串是否為空
void resize(int len,char c); //把字符串當前大小置為len,多去少補,多出的字符c填充不足的部分
測試代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
if (str.empty())
cout<< "str is NULL." <<endl;
else
cout<< "str is not NULL." <<endl;
str = str + "abcdefg" ;
cout<< "str is " <<str<<endl;
cout<< "str‘s size is " <<str.size()<<endl;
cout<< "str‘s capacity is " <<str.capacity()<<endl;
cout<< "str‘s max size is " <<str.max_size()<<endl;
cout<< "str‘s length is " <<str.length()<<endl;
str.resize(20, ‘c‘ );
cout<< "str is " <<str<<endl;
str.resize(5);
cout<< "str is " <<str<<endl;
return 0;
}
|
程序執行結果為:
str is NULL.
str is abcdefg
str‘s size is 7
str‘s capacity is 15
str‘s max size is 4294967294
str‘s length is 7
str is abcdefgccc
str is abcde
四、string的查找
由於查找是使用最為頻繁的功能之一,string提供了非常豐富的查找函數:(註:string::npos)
size_type find( const basic_string &str, size_type index ); //返回str在字符串中第一次出現的位置(從index開始查找),如果沒找到則返回string::npos
size_type find( const char *str, size_type index ); // 同上
size_type find( const char *str, size_type index, size_type length ); //返回str在字符串中第一次出現的位置(從index開始查找,長度為length),如果沒找到就返回string::npos
size_type find( char ch, size_type index ); // 返回字符ch在字符串中第一次出現的位置(從index開始查找),如果沒找到就返回string::npos
註意:查找字符串a是否包含子串b,不是用 strA.find(strB) > 0 而是 strA.find(strB) != string:npos 這是為什麽呢?(初學者比較容易犯的一個錯誤)本部分參考自web100與luhao1993
先看下面的代碼
int idx = str.find("abc"); if (idx == string::npos);
上述代碼中,idx的類型被定義為int,這是錯誤的,即使定義為 unsigned int 也是錯的,它必須定義為 string::size_type。npos 是這樣定義的: static const size_type npos = -1; 因為 string::size_type (由字符串配置器 allocator 定義) 描述的是 size,故需為無符號整數型別。因為缺省配置器以型別 size_t 作為 size_type,於是 -1 被轉換為無符號整數型別,npos 也就成了該型別的最大無符號值。不過實際數值還是取決於型別 size_type 的實際定義。不幸的是這些最大值都不相同。事實上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是兩者型別大小不同)。因此,比較式 idx == string::npos 中,如果 idx 的值為-1,由於 idx 和字符串string::npos 型別不同,比較結果可能得到 false。因此要想判斷 find()等查找函數的結果是否為npos,最好的辦法是直接比較。
測試代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<iostream>
#include<string>
using namespace std;
int main(){
int loc;
string s= "study hard and make progress everyday! every day!!" ;
loc=s.rfind( "make" ,10);
cout<< "the word make is at index" <<loc<<endl; //-1表示沒找到
loc=s.rfind( "make" ); //缺省狀態下,從最後一個往前找
cout<< "the word make is at index" <<loc<<endl;
loc=s.find_first_of( "day" );
cout<< "the word day(first) is at index " <<loc<<endl;
loc=s.find_first_not_of( "study" );
cout<< "the first word not of study is at index" <<loc<<endl;
loc=s.find_last_of( "day" );
cout<< "the last word of day is at index" <<loc<<endl;
loc=s.find( "day" ); //缺陷狀態下從第一個往後找
cout<<loc;
return 0;
}
|
運行結果:
五、其他常用函數
string &insert(int p,const string &s); //在p位置插入字符串s
string &replace(int p, int n,const char *s); //刪除從p開始的n個字符,然後在p處插入串s
string &erase(int p, int n); //刪除p開始的n個字符,返回修改後的字符串
string substr(int pos = 0,int n = npos) const; //返回pos開始的n個字符組成的字符串
void swap(string &s2); //交換當前字符串與s2的值
string &append(const char *s); //把字符串s連接到當前字符串結尾
void push_back(char c) //當前字符串尾部加一個字符c
const char *data()const; //返回一個非null終止的c字符數組,data():與c_str()類似,用於string轉const char*其中它返回的數組是不以空字符終止,
const char *c_str()const; //返回一個以null終止的c字符串,即c_str()函數返回一個指向正規C字符串的指針, 內容與本string串相同,用於string轉const char*
測試代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "abc123defg" ;
string str2 = "swap!" ;
cout<<str1<<endl;
cout<<str1.erase(3,3)<<endl; //從索引3開始的3個字符,即刪除掉了"123"
cout<<str1.insert(0, "123" )<<endl; //在頭部插入
cout<<str1.append( "123" )<<endl; //append()方法可以添加字符串
str1.push_back( ‘A‘ ); //push_back()方法只能添加一個字符
cout<<str1<<endl;
cout<<str1.replace(0,3, "hello" )<<endl; //即將索引0開始的3個字符替換成"hello"
cout<<str1.substr(5,7)<<endl; //從索引5開始7個字節
str1.swap(str2);
cout<<str1<<endl;
const char * p = str.c_str();
printf ( "%s\n" ,p);
return 0;
}
|
程序執行結果為:
abc123defg
abcdefg
123abcdefg
123abcdefg123
123abcdefg123A
helloabcdefg123A
abcdefg
swap!
swap!
【C++】C++string類總結