1.String概念
string是STL的字符串類型,通常用來表示字符串。而在使用string之前,字符串通常是用char*表示的。
string和char*的區別:
string是一個類, char*是一個指向字符的指針。
string封裝了char*,管理這個字符串,是一個char*型的容器。也就是說string是一個容器,里面元素的數據類型是char*。string不用考慮內存釋放和越界。
string管理char*所分配的內存。每一次string的復制,取值都由string類負責維護,不用擔心復制越界和取值越界等。string提供了一系列的字符串操作函數 查找find,拷貝copy,刪除erase,替換replace,插入insert
2.初始化–構造函數
- 默認構造函數 :
string();//構造一個空的字符串string s1
。 - 拷貝構造函數:
string(const string &str);//構造一個與str一樣的string。如string s1(s2)
。 - 帶參數的構造函數 :
string(const char *s); //用字符串s初始化
string(int n,char c); //用n個字符c初始化
。
#include <iostream>
#include <string>
#include <algorithm>using namespace std;
// string的構造
void func1()
{string s1 = "hello";string s2("world");string s3 = s2; // 拷貝構造string s4(s1); // 拷貝構造string s5(10,'a'); // 字符串長度為10,每個值都是'a'cout << s1 << endl;cout << s2 << endl;cout << s3 << endl;cout << s4 << endl;cout << s5 << endl;
}int main()
{func1();system("pause");return 0;
}
3.存取字符
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()在越界時會拋出異常,[]在剛好越界時會返回(char)0,再繼續越界時,編譯器直接出錯。如果你的程序希望可以通過try,catch捕獲異常,建議采用at()。
string的長度
int length() const; //返回當前字符串的長度。長度不包括字符串結尾的'\0'。
bool empty() const; //當前字符串是否為空
string遍歷
string的遍歷可以分為數組方式和使用迭代器兩種方式。
// 遍歷
void func2()
{string str = "hello world";// 1、用數組下標的方式for (unsigned int i = 0; i < str.length(); i++){cout << str[i] << " ";}cout << endl;// 2、通過迭代器string::iterator it;for (it = str.begin(); it != str.end(); it++){cout << *it << " ";}cout << endl;// at(index) 函數for (unsigned int i = 0; i < str.length(); i++){cout << str.at(i) << " ";}cout << endl;// [] 和 at 區別:當數組越界的時候,[] 會直接讓程序崩掉、at會拋出異常{try{for (unsigned int i = 0; i < str.length()+10; i++){// cout << str[i] << " ";cout << str.at(i) << " ";}cout << endl;}catch (exception &e){printf ("捕獲一個異常: %s\n", e.what());}}}
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個字符賦給當前字符串
4.和char*類型的轉換
從string轉換到char*的成員函數主要是:
const char *c_str() const; //返回一個以’\0’結尾的字符串的首地址
把string拷貝到char*指向的內存空間的成員函數是:
int copy(char *s, int n, int pos=0) const;
把當前串中以pos開始的n個字符拷貝到以s為起始位置的字符數組中,返回實際拷貝的數目。
注意要保證s所指向的空間足夠大以容納當前字符串,不然會越界。
/ string ---> char*
// char * ---> string
void func4()
{string str = "hello world";// 返回 string 字符串的 char *類型指針const char *ps = str.c_str();printf("str = %s\n", ps);
}
5.比較操作
int compare(const string &s) const; //與字符串s比較
int compare(const char *s) const; //與字符串s比較
compare函數在>時返回 1,<時返回 -1,==時返回 0。比較區分大小寫,比較時參考字典順序,排越前面的越小。大寫的A比小寫的a小。
6.string的連接和復制
// 連接和復制
void func3()
{string str1 = "hello";string str2 = " world";// 兩個字符串的連接string s3 = "1234 " + str1 + str2 + " abc";str1 += " 1234";cout << str1 << endl;cout << s3 << endl;// 復制string s4;s4 = str1; // 重載 = 運算符cout << s4 << endl;// 對數組進行 復制 string ---> char []char c[100] = {0};str1.copy(c, 5);cout << c << endl;
}
7.查找和替換
// 查找和替換
void func5()
{string str = "123 hello 456 hello 789 hello abc hello";// 查找子串// 找到的是第一個相匹配的字符串的下標int index = str.find("ahello", 0);if (index != string::npos)cout << index << endl;index = str.find("hello", 0);while (index != string::npos){cout << index << endl;index = str.find("hello", index+1);}// 替換string str2 = "123 abc asdasdsa";str2.replace(1, 2, "xxx");cout << str2 << endl;index = str.find("hello", 0);while (index != string::npos){str.replace(index, 5, "HELLO");index = str.find("hello", index+1);}cout << str << endl;
}
8.刪除和插入
// 刪除
void func6()
{string str = "123 hello 456 hello 789 hello abc hello";// 通過迭代器刪除某一個元素str.erase(str.begin()+ 8);cout << str << endl;// 1、str.findint index = str.find('b', 0);if (index != string::npos)str.erase(str.begin()+index);cout << str << endl;// 2、通過 查找算法 去 某個字符,算法 操作都是迭代器 // 和 str 內置的 str.find 區分開 find 是算法庫提供的函數 ,返回的是一個迭代器,指向找到的元素string::iterator it = find(str.begin(), str.end(), '9');if (it != str.end())str.erase(it);cout << str << endl;str.erase(4, 5); // 從下標為 4 的位置開始,刪除5個元素,如果沒有第二個參數,刪除4之后所有的元素cout << str << endl;// 區間刪除string str1 = "123 hello 456 hello 789 hello abc hello";// 刪除方式 是 左閉 又開的 [begin, end)str1.erase(str1.begin(), str1.begin()+4);str1.erase(str1.begin(), str1.end());cout << str1 << endl;
}
9.算法相關
使用transform函數將string里面的字符進行大小寫的轉換(具體的STL的算法另作介紹):
#define _CRT_SECURE_NO_WARNINGS#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <string>using namespace std;void play()
{string s1 = "AAAbbb";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;
}int main()
{ play();system("pause");return 1;
}