string的模擬實現
- string的基本用法
- string的遍歷(三種方式):
- 關于auto(自動推導):
- 范圍for:
- 迭代器
- 普通迭代器(可讀可改)
- const迭代器(可讀不可改)
- string細小知識點
- string的常見接口
- 引用計數和寫時拷貝(了解)
- 編碼(了解)
string的基本用法
#include<iostream>
#include<string>//string要包含頭文件`#include<string>
using namespace std;int main()
{string st1;//無參string st2("1111");//帶參string st3(st2);//拷貝構造cout << st1 << endl;cout << st2 << endl;cout << st3 << endl;string st4(st2, 2);cout << st4 << endl;string st5("hello world", 3);cout << st5 << endl;string st6(10, 'x');cout << st6 << endl;string st7("hello world");//opterator[],可以讀和修改st7[0] = 'x';cout << st7 << endl;}
運行結果:
string的遍歷(三種方式):
//1.下標+[]for (size_t i = 0; i < st7.size(); i++){cout << st7[i];}cout << endl;//2.迭代器(所有容器都可以用類似的方式)string::iterator it = st7.begin();while (it != st7.end()){cout << *it;it++;}cout << endl;//3.范圍for(),自動賦值,自動迭代,自動判斷結束for (auto ch:st7)//auto代表自動推導類型{cout << ch<<" ";}cout << endl;
關于auto(自動推導):
主要用途:替換長類型,簡化代碼,但一定程度上犧牲了代碼的可讀性。
auto不能作為函數的參數,可以做返回值,但是建議謹慎使用。
// 不能做參數
void func2(auto a)
{}// 可以做返回值,但是建議謹慎使用
auto func3()
{return 3;
}
auto不能直接用來聲明數組。
// 編譯報錯:error C3318: “auto []”: 數組不能具有其中包含“auto”的元素類型auto array[] = { 4, 5, 6 };
范圍for:
適用于容器和數組的遍歷。
int main()
{int array[] = { 1, 2, 3, 4, 5 };// C++98的遍歷for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i){array[i] *= 2;}for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i){cout << array[i] << endl;}// C++11的遍歷for (auto& e : array)e *= 2;for (auto e : array)cout << e << " " << endl;string str("hello world");for (auto ch : str){cout << ch << " ";}cout << endl;return 0;
}
迭代器
普通迭代器(可讀可改)
1.正向迭代器
string s1("hello world");string::iterator it = s1.begin();while (it != s1.end()){cout << *it;it++;}cout << endl;
2.反向迭代器
string s1("hello world");
string::reverse_iterator rit = s1.rbegin();while (rit != s1.rend()){cout << *rit;rit++;}cout << endl;
const迭代器(可讀不可改)
1.const正向迭代器
const string s1("hello world");
string::reverse_iterator cit = s1.begin();while (cit != s1.end()){cout << *cit;cit++;}cout << endl;
2.const反向迭代器
const string s1("hello world");
string::const_reverse_iterator sit = s1.rbegin();while (sit != s1.rend()){cout << *sit;sit++;}cout << endl;
string細小知識點
開空間:reserve
string s1("hello world");s1.reserve(100);//提前開好空間,避免擴容 reserve “保留”
求string的長度:size()(通用),length()(不具有通用性,C++向前兼容)
string s1("hello world");
cout << s1.size() << endl;
string的常見接口
void test()
{string s("hello world");s.push_back('x');//一次只能插入一個s += 'g';s += "hhh";//一般尾插用這個//頭插s.insert(0, "nihao");cout << s << endl;
}void test2()
{string s("hello world");s.erase(1, 1);//尾刪s.erase(s.size() - 1, 1);cout << s << endl;string s1("hello world");//替換s1.replace(5, 1,"%%");cout << s1 << endl;//替換所有的空格string s3("hello world");string tmp;for (auto ch : s3){if (ch == ' '){tmp += "%d%d";}else{tmp += ch;}}cout << tmp << endl;void test3()
{string s("test.cpp.zip");size_t pos = s.find('.');string s1 = s.substr(pos);cout << s1 << endl;size_t pos1 = s.rfind('.');string s2 = s.substr(pos1);cout << s2<< endl;
}void test4()
{string s("hello");string s1 = s + "world";cout <<s1<< endl;getline(cin, str);//遇到換行才會停止
}}
int main()
{test();return 0;
}
引用計數和寫時拷貝(了解)
寫時拷貝就是一種拖延癥,是在淺拷貝的基礎之上增加了引用計數的方式來實現的。
引用計數:用來記錄資源使用者的個數。在構造時,將資源的計數給成1,每增加一個對象使用該資源,就給計數增加1,當某個對象被銷毀時,先給該計數減1,然后再檢查是否需要釋放資源,如果計數為1,說明該對象時資源的最后一個使用者,將該資源釋放;否則就不能釋放,因為還有其他對象在使用該資源。
編碼(了解)
編碼:值和符號的映射關系
ASCII編碼表本質是:英文符號和值的映射關系
統一編碼:Unicode(萬國碼)
了解UTF-8,UTF-16,UTF-32;
GBK(國標):中國自己做了一套自己的編碼;