1.string是表示字符串的字符串類
2.該類的接口與常規容器的接口基本相同。
string類對象的常見構造
void TestString()
{
string s1;//構造一個string空類對象s1
string s2("hello bit");//用c字符串的形式構造一個string類s2
string s3(10, 'a');//用10個字符‘a’構造string類對象s3
string s4(s2);//拷貝構造s4
string s5(s3, 5);//用s3中前5個字符構造string對象s5
}
string類對象的容量操作
void TestString1()
{
string s("hello world");
//string s;
cout << s.length() << endl;
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
//將s中的字符串清空,注意情空時只是將size清0,不改變底層空間的大小
s.clear();
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
//將s中有效字符個數增加到10個,多出位置用‘a’進行填充
//‘aaaaaaaaaa’
s.resize(10,'a');
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
//將s中有效字符個數增加到15個,多出位置用缺省值‘\0’進行填充
//'aaaaaaaaaa\0\0\0\0\0'
//注意此時s中有效字符個數已經增加到15個
//s.resize(15,'1');
s.resize(15);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
//將s中的有效字符個數縮小到5個
s.resize(5);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
//增大
s.resize(16);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
}
void TestString2()
{
string s;
//測試reserve是否會改變string中有效元素的個數
s.reserve(100);
cout << s.size() << endl;
cout << s.capacity() << endl;
//測試resever參數小於string的底層大小時,是否會將空間縮小
s.reserve(50);
cout << s.size() << endl;
cout << s.capacity() << endl;
}
1.size()與length()方法底層實現原理完全相同,引入size的原因是爲了與其他容器接口保持一致。
2.clear只是將string中有效字符清空,不改變底層空間大小。
3.resize(size_t n,char c)將字符串中的有效字符個數改變爲nge,用字符c來填充多出來元素,當元素個數增多,可能會改變底層容量的大小。
4.reserve,爲string預留空間,不改變有效元素的個數,當reserve的參數小於string的底層空間總大小時,reserver不會改變容量大小。reserve就是一個增容作用,自動增容,第一次擴大2倍,後續按1.5倍增容。提高了效率,減少了增容的操作。
string類對象的訪問操作
void TestString()
{
string s1("hello world");
const string s2("hello world");
cout<<s1<< " "<< s2 << endl;
cout << s1[0] << " " << s2[0] << endl;
s1[0] = 'H';
cout << s1 << endl;
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1[i] << endl;
}
//s2[0]='H' ; 編譯失敗,因爲const類型對象不能修改
}
string類對象的修改操作
void TestString()
{
string s1("hello world");
const string s2("hello world");
cout<<s1<< " "<< s2 << endl;
cout << s1[0] << " " << s2[0] << endl;
s1[0] = 'H';
cout << s1 << endl;
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1[i] << endl;
}
//s2[0]='H' ; 編譯失敗,因爲const類型對象不能修改
}
void TestString2()
{
string str;
str.push_back(' ');//在str後面插入空格
str.append("hello");//在str後面追加一個字符串hello
str += 'b';//在str後追加一個字符‘b’
str += "it";//在str後追加一個字符串“it”
cout << str << endl;
cout << str.c_str() << endl;//以c語言的方式來打印字符串
//獲取file的後綴
string file("string.cpp");
size_t pos = file.rfind('.');//從後開始找字符爲.
string suffix(file.substr(pos, file.size() - pos));//截取字符串
cout << suffix << endl;
//nops是string裏面的一個靜態成員變量
//static const size_t npos=-1;
//取出url中的域名
//url統一資源定位符
string url("http://www.cplusplus.com/reference/string/string/find/");
cout << url << endl;
size_t start = url.find("://");//找到://的位置
if (start == string::npos)
{
cout << "invalid url" << endl;
return;
}
start += 3;
size_t finish = url.find('/', start);//從start位置開始找到第一個/的位置
string address = url.substr(start, finish - start);
cout << address << endl;
//刪除url的協議前綴
pos = url.find("://");
url.erase(0, pos + 3);
cout << url << endl;
}
//利用reserve提高插入數據的效率,避免增容帶來的開銷
//
void TestPushBack()
{
string s;
size_t sz = s.capacity();
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s += 'c';
cout << s << endl;
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed:" << sz << '\n';
}
}
}
1.在string尾部追加字符時,s.push_back / s.append() /s+=‘'三種的實現方式差不多,一般情況下string類的+=操作比較多,+=操作不僅可以連接單個字符,還還可以連接字符串。
2.對string操作,如果能夠大概預估到放多少字符,可以先通過reserve把空間預留好。