string的底层
class string
{private:char _buff[16];char* _str;size_t _size;size_t _capacity;};
初始化
void test()
{string s1;string s2("hello world");string s3(s2);//空stringcout << s1 << endl;//用一个字符串初始化 const char* scout << s2 << endl;//用一个string进行初始化cout << s3 << endl;string s4(s2, 6, 20);//用string的一部分初始化cout << s4 << endl;string s5(s2, 6);//cout << s5 << endl;string s6("hello world", 5);//用字符串的前5个初始化cout << s6 << endl;string s7(10, 'x');//用10个 字符进行初始化cout << s7 << endl;
}
int main()
{test();return 0;
}
遍历一个string
#include<map>
#include<list>
void test_string1()
{string s1;string s2("hello world");cout << s1 << s2 << endl;s2[0] = 'x';//1.下标+[]for (size_t i = 0; i < s2.size(); i++){cout << s2[i] << ' ';}cout << endl;//2.迭代器string::iterator it = s2.begin();while (it != s2.end()){cout << *it << ' ';++it;}cout << endl;//范围for 底层就是迭代器 自动迭代,自动判断结束for (auto e : s2){cout << e << ' ';}cout << endl;map<string, string>dict;//map<string, string>::iterator mit = dict.begin();auto mit = dict.begin();//遍历链表只能使用迭代器list<int> lt = { 1,2,3,4,5,6,7 };list<int>::iterator lit = lt.begin();while (lit != lt.end()){cout << *lit << ' ';++lit;}cout << endl;
}
int main()
{//test();test_string1();return 0;
}
迭代器的种类
void test_string2()
{string s1;string s2("hello world");//正向迭代器string::iterator it = s2.begin();while (it != s2.end()){cout << *it << ' ';++it;}cout << endl;//反向迭代器string::reverse_iterator rit = s2.rbegin();while (rit != s2.rend()){cout << *rit << ' ';++rit;}cout << endl;//const迭代器string::const_iterator cit = s2.cbegin();while (cit != s2.end()){cout << *cit << ' ';cit++;}cout << endl;//const反向迭代器string::const_reverse_iterator crit = s2.crbegin();while (crit != s2.crend()){cout << *crit << ' ';++crit;}cout << endl;
}
size capacity reserve
void test_string3()
{//string s1;//string s2("hello world");//cout << s2.length() << endl;//cout << s2.size() << endl;//cout << s2.max_size() << endl;//cout << s2.capacity() << endl;//string s3("xxxxxx");//cout << sizeof(s2) << endl;//cout << sizeof(s3) << endl;//char _buff[16];//char* _str;//size_t _size;//size_t _capacity;string s1;size_t sz = s1.capacity();reserve 扩容reverse 反转逆置//1.5倍扩容s1.reserve(94);cout << "capacity:" << sz << endl;for (size_t i = 0; i < 100; i++){s1 += 'x';if (sz != s1.capacity()){sz = s1.capacity();cout <<"grow capacity:" << sz << endl;}}
}
reserve clear
void test_string4()
{string s1("hello worldxxxxsssssssss");//vs下不缩容s1.reserve(40);cout << s1.size() << endl;cout << s1.capacity() << endl;//24 47s1.reserve(30);cout << s1.size() << endl;cout << s1.capacity() << endl;s1.reserve(10);cout << s1.size() << endl;cout << s1.capacity() << endl;s1.reserve(60);cout << s1.size() << endl;cout << s1.capacity() << endl;//清数量 不清容量s1.clear();cout << s1.size() << endl;cout << s1.capacity() << endl;
}
auto
//可以做返回值
int fun1()
{return 10;
}auto fun2()
{return fun1();
}
//不能做参数
//auto fun3(auto a = 10)
//{
// return a;
//}int main()
{auto a = 1;auto b = a;auto c = 'x';auto e = fun1();//auto e;cout << typeid(a).name() << endl;cout << typeid(b).name() << endl;cout << typeid(c).name() << endl;//一行推导为同一类型auto a = 1, b = 1;//auto a = 1.0, b = 1;return 0;
}
push_back append += insert
void test_string5()
{string s("hello world");////push_back尾插一个字符//s.push_back('y');//s.push_back('m');//for (auto e : s)//{// cout << e << ' ';//}//cout << endl;////append//s.append("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");//for (auto e : s)//{// cout << e << ' ';//}//cout << endl;////+=//s += 'o';//s += "xooooo";//for (auto e : s)//{// cout << e << ' ';//}//cout << endl;//insert 字符串插入 字符插入s.insert(0, "xxxx");cout << s << endl;s.insert(10, "zzzz");cout << s << endl;s.insert(0, "p");cout << s << endl;char ch = 't';s.insert(0, 1, ch);s.insert(s.begin(), ch);}
erase replace
void test_string6()
{string s("hello world");//s.erase(0, 6);//cout << s << endl;//s.erase(1);//cout << s << endl;//头删s.erase(0, 1);//位置 数量s.erase(s.begin());//位置//尾删s.erase(s.size() - 1,1);s.erase(s.end()--);//cout << s << endl;string ss("hello world");size_t pos = ss.find(' ');while (pos != string::npos){ss.replace(pos, 1, "%%");pos = ss.find(' ', pos + 2);}cout << ss << endl;string tmp;tmp.reserve(ss.size());for (auto e : ss){if (e == ' '){tmp += "%%";}else{tmp += e;}}ss.swap(tmp);cout << tmp << endl;string file;cin >> file;FILE* fout = fopen(file.c_str(), "r");char ch = fgetc(fout);while (ch != EOF){cout << ch;ch = fgetc(fout);}fclose(fout);
}
find系列
void test_string7()
{//string s("test.cpp.zip");//size_t pos = s.find('.');//string subbfix = s.substr(pos);//cout << subbfix << endl;//string s("test.cpp.zip");//size_t pos = s.rfind('.');//string subbfix = s.substr(pos);//cout << subbfix << endl;//string ss("ciefbweahfnhoiwehfwJFPQEjfpqejfpQOFJPOjfwefjewioafjewiojfewioafjoiewajf");//size_t pos = ss.find_first_of("abcdef");//while (pos != EOF)//{// ss[pos] = '*';// pos = ss.find_first_of("abcdef", pos + 1);//}//cout << ss << endl;string str1("/usr/bin/man");string str2("c:\\windows\\winhelp.exe");size_t pos = str1.find_last_of("/\\");cout << "path:" << str1.substr(0, pos) << endl;cout << "file:" << str1.substr(pos + 1) << endl;}
+
void test_string8()
{string s1("hello ");string s2 = s1 + "world";cout << s2 << endl;string s3 = "world" + s1;cout << s3 << endl;
}