string和char*都可以表示字符串:
string是一個類,char*是一個指向字符的指針。string封裝了char*,管理這個字符串,是一個char*類型的容器。
string管理char*所分配的內存,不用考慮內存釋放和越界。
string的構造函數
- string(); //默認構造函數,構造一個空的字符串
- string(const string &str); //拷貝構造函數,構造一個與str一樣的string。
- string(const char *s); //用字符串s初始化
- string(int n,char c); //用n個字符c初始化
string的賦值
- string &operator=(const string &s);//把字符串s賦給當前的字符串
- string &assign(const char *s); //把字符串s賦給當前的字符串
- string &assign(const char *s, int n); //把字符串s的前n個字符賦給當前的字符串
- string &assign(const string &s); //把字符串s賦給當前字符串
- string &assign(int n,char c); //用n個字符c賦給當前字符串
- string &assign(const string &s,int start, int n); //把字符串s中從start開始的n個字符賦給當前字符串
string的初始換
string s1 = "zhangsan";
string s2("lisi");
string s3 = s1;//拷貝構造函數
string s4(3,"a");
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
cout << "s4:" << s4 << endl;
string的存取字符操作
- const char &operator[] (int n) const;
- const char &at(int n) const;
- char &operator[] (int n);
- char &at(int n);
這裏注意operator[]和at()都返回字符串的第n個字符,但是使用at()時如果出現字符串數組下標越界會拋出異常,而operator[]直接會編譯器編譯不通過報錯。
string s = "123456789";
//數組
for(int i = 0; i < s.length(); i++)
{
cout << s[i] << endl;
}
//迭代器
for(string::iterator it = s.begin(); it < s.end(); it++)
{
cout<< *it << endl;
}
{
//使用at()時會拋出異常
try
{
for(int i = 0; i < s.length()+1; i++)
{
cout << s.at(i) << endl;
}
}
catch(...)
{
cout<< "數組越界" << endl;
}
//出現數組越界引起程序的中斷,不會拋出異常 error
try
{
for(int i = 0; i < s.length()+1; i++)
{
cout << s[i] << endl;
}
}
catch(...)
{
cout<< "數組越界" << endl;
}
}
從string取得const char*的操作
- const char *c_str() const; //返回一個以'\0'結尾的字符串的首地址
- int copy(char *s, int n, int pos=0) const;
字符指針和string的轉換
string s = "abc";// char* ==> string
printf("s:%s \n", s.c_str());// string ==>char*
char buf[128];
s.copy(buf,2,0);//把string的數據copy到字符數組中
string的長度
- int length() const; //返回當前字符串的長度。長度不包括字符串結尾的'\0'。
- bool empty() const; //當前字符串是否爲空
string字符串連接
- string &operator+=(const string &s); //把字符串s連接到當前字符串結尾
- string &operator+=(const char *s);//把字符串s連接到當前字符串結尾
- string &append(const char *s); //把字符串s連接到當前字符串結尾
- string &append(const char *s,int n); //把字符串s的前n個字符連接到當前字符串結尾
- string &append(const string &s); //同operator+=()
- string &append(const string &s,int pos, int n);//把字符串s中從pos開始的n個字符連接到當前字符串結尾
- string &append(int n, char c); //在當前字符串結尾添加n個字符c
string s1 = "zhang";
string s2 = "san";
s1 += s2;
cout<< "s1:" << s1 <<endl;
string s3 = "li";
string s4 = "si";
s3.append(s4);
cout<< "s3:" << s3 << endl;
string的查找和替換
查找
- int find(char c,int pos=0) const; //從pos開始查找字符c在當前字符串的位置
- int find(const char *s, int pos=0) const; //從pos開始查找字符串s在當前字符串的位置
- int find(const string &s, int pos=0) const; //從pos開始查找字符串s在當前字符串中的位置
find函數如果查找不到,就返回-1
- int rfind(char c, int pos=npos) const; //從pos開始從後向前查找字符c在當前字符串中的位置
- int rfind(const char *s, int pos=npos) const;
- int rfind(const string &s, int pos=npos) const;
//rfind是反向查找的意思,如果查找不到, 返回-1
替換
- string &replace(int pos, int n, const char *s);//刪除從pos開始的n個字符,然後在pos處插入串s
- string &replace(int pos, int n, const string &s); //刪除從pos開始的n個字符,然後在pos處插入串s
- void swap(string &s2); //交換當前字符串與s2的值
//查找
string s = "Hello world !!!";
//從位置下標0開始,找到第一次出現“l”的位置
int index = s.find("l",0);
cout<<"index :"<<index<<endl;
//求出“l”出現的次數 每一次出現的數組下標
int offindex = s.find("l",0);
while(offindex != string::npos)
{
cout<<"offindex"<<offindex<<endl;
offindex += 1;
offindex = s.find("l",offindex);
}
//替換
string s1 = "Hello world !!!";
s1.replace(0,3,"HEL");
cout<<"s1:"<<s1<<endl;
offindex = s.find("l",0);
while(offinxes != string::npos)
{
cout<<"offindex:"<<offindex<<endl;
s.replace(offindex,1,"L");
offindex += 1;
offindex = s.find("l",offindex);
}
cout<<"替換後s:"<< s<<endl;
String的區間刪除和插入
- string &insert(int pos, const char *s);
- string &insert(int pos, const string &s);
//前兩個函數在pos位置插入字符串s
- string &insert(int pos, int n, char c); //在pos位置 插入n個字符c
- string &erase(int pos=0, int n=npos); //刪除pos開始的n個字符,返回修改後的字符串
//刪除
string s = "cpp1 cpp2 cpp3";
string::iterator it = find(s.begin(),s.end(),"c");
if(it != s.end())
{
s.erase(it);
}
cout<<"刪除“c”以後的結果s:"<<s<<endl;
s.erase(s.begin(),s.end());
cout<<"s全部刪除:"<<s<<endl;
cout<<"s的長度:"<<s.length()<<endl;
//插入
string s1 = "bbb";
s1.insert(0,"AAA");//頭插法
s1.insert(s1.lenght(),"CCC");//尾插法
cout<<"s1:"<<s1<<endl;
string算法相關
//小寫轉大寫
string s1 = "AAAbbb";
//1函數的入口地址 2函數對象 3預定義的函數對象
transform(s1.begin(), s1.end(), s1.begin(), toupper);
cout << "s1" << s1 << endl;
//大寫轉小寫
string s2 = "AAAbbb";
transform(s2.begin(), s2.end(), s2.begin(), tolower);
cout << "s2:" << s2 << endl;