(一)string的理解
string是一个容器,主要注意初始化、字符的增删改查(注意类型)、迭代遍历(方式很多)、内存管理和一些字符串函数等等。
内存管理resize 和reserv分析见https://blog.csdn.net/oppo62258801/article/details/75949802
代码见下。
(二)Test Demo
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
cout << "******************Demo1:string初始化**************************" << endl;
string str1, strline;
string str2 = str1;
string str3(str1);
string str4 = "string"; //不包含最后的‘\0’
string str5("string");//不包含最后的‘\0’
string str6(10, 'a');//连续的10个a 组成的string
cout << "******************Demo2:string操作**************************" << endl;
cout << "(1)cin && cout:>> <<" << endl;
cin >> str1;
cout << "str1 = " << str1 << endl;
cout << "(2)getline(cin, string)" << endl;
/*前部分用cin函数一个一个的读入数据,中间部分利用getline()一起读入一行,
*测试发现,cin之后的getline函数并无作用,原来cin只是在缓存区中,把字符读走,会剩余/n在缓存区中,
*但是getline对/n极度敏感,导致getline刚开始读入便遇到/n于是停止读入数据。
*解决办法:在使用getline()前使用cin.ignore();
*/
cin.ignore();
getline(cin, strline);
cout << strline << endl;
cout << "(3)str.empty() && str.size" << endl;
if (str1.empty() == true)
cout << "str1 is empty" << endl;
else
cout << "str1 is not empty, ane the size is " << str1.size() << endl;
cout << "(4)str = == != > < >= <=" << endl;
cout << "(5)string 遍历" << endl;
/*.C str[index] 方式也可以..*/
string s1 = "hello", s2 = "sas";
s1 = s2;
for (size_t i = 0; i < s1.length(); i++)
{
cout << s1.at(i);
}
cout << endl;
for (auto c:s1) {
cout << c;
}//output:sss
cout << endl;
//默认auto后的item类型为string::iterator,当然也可以用string::const_iterator
for (auto item = s1.begin(); item != s1.end(); ++item) {
cout << *item;
}
cout << endl;
cout << "(6)string 函数" << endl;
cout << "s1 = " << s1 << endl;
cout << "find sub string 'as' in s1, the index is:" << s1.find("as", 0) << endl;
cout << "(7)string insert/append/push_back/erase" << endl;
string new_str("12345");
char c;
cout << "str = " << new_str << endl << "now push_back a c:";
cin >> c;
new_str.append((const char *)("c"));
new_str.push_back(c);
cout << "now:" << new_str << endl << "now insert the c:";
string::const_iterator pos = new_str.begin();
new_str.insert(pos, c);
cout << "now:" << new_str << endl << "now erase the insert c:";
new_str.erase(pos);
cout << "now:" << new_str << endl;
cout << "(8)string 内存管理相关resize/reserv" << endl;
string s_size("123");
cout << "str = " << s_size << endl << "size = " << s_size.size() << endl << "cap* = " << s_size.capacity() << endl;
s_size.resize(20);
cout << "str = " << s_size << endl << "size = " << s_size.size() << endl << "cap* = " << s_size.capacity() << endl;
s_size.reserve(50);//内配方式:n*16 - 1
cout << "str = " << s_size << endl << "size = " << s_size.size() << endl << "cap* = " << s_size.capacity() << endl;
system("pause");
return 0;
}
(三)string源码追踪
(1)string str;
(2)string define in here
using string = basic_string<char, char_traits<char>, allocator<char>>;<<======================
using wstring = basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>>;
using u16string = basic_string<char16_t, char_traits<char16_t>, allocator<char16_t>>;
using u32string = basic_string<char32_t, char_traits<char32_t>, allocator<char32_t>>;
(3)basic_string smiple define in here
template<class _Elem,
class _Traits = char_traits<_Elem>,
class _Alloc = allocator<_Elem>>
class basic_string
: public _String_alloc<_String_base_types<_Elem, _Alloc>>
{/*opt 2000行左右!!!*/};
核心在于:using string = basic_string<char, char_traits<char>, allocator<char>>;
给basic_string 这个类模板传入三个参数,使得string的行为被定义为分配char型的内存块,以及各种接口的特性等,没细看!