1.标准库中的string类
1.构造函数的使用方式
#include<string>
using namespace std;
void test()
{
/*default (1)
string(); //无参构造*/
//创建空的string对象
string str;
/*copy(2) //拷贝构造
string(const string& str);*/
string copy(str);
/*from c - string(4)
string(const char* s);*/
string str2("abc");
/*substring(3)
string(const string& str, size_t pos, size_t len = npos);*/
string substr(str2, 1,2); //"bc"
/*from sequence(5)
string(const char* s, size_t n);*/
string str3("12345678",5); //12345
/*fill(6)
string(size_t n, char c);*/
string str4(10, 'a');//"aaaaaaaaaa"
string str5 = "abcde";//但参构造的隐式类型转换
}
2.构造函数的访问方式
void test()
{
const string str = "12345";
string str2 = "12345";
//const对象 调用接口:const char& operator[](size_t pos) const
char ch = str[3];
ref = 'c';
str[3] = 'a';
//非const对象,char& operator[](size_t pos)
char& ref = str2[3];
str2[3] = 'a';
str2.at[3] = 'b';
//[]越界:报断言错误
//char ch2 = str2[10];
//at接口越界:抛异常
char ch2 = str2.at(10);
}
3.迭代器 访问方式
string的访问方式:
1.for循环 +operator[]
2.迭代器
3.范围for
这三种方式都可以修改内容
void test()
{
//正向迭代器
string str = "12345";
//起始位置的迭代器
string::iterator it = str.begin();
for (; it != str.end(); ++it)
{
//迭代器的解引用
cout << *it << " ";
//可以通过迭代器进行内容的修改
*it = 'a';
}
const string str2 = "abcde";
//只读迭代器
string::const_iterator it2 = str2.begin();
while (it2 != str2.end())
{
cout << *it2 << " ";
//只读迭代器不能修改内容
//*it2 = '1';
++it2;
}
}
void test()
{
//反向迭代器
string str = "12345";
//最后一个元素的位置
string::reverse_iterator it = str.rbegin();
while (it != str.rend())
{
cout << *it << " "; //54321
*it = 'a';
//反向移动
++it;
}
string::const_reverse_iterator cit = str.crbegin();
while (cit != str.crend())
{
cout << *cit << " ";
//*cit = 'a';
++cit;
}
//只读的反向迭代器
const string str2 = "abcde";
string::const_reverse_iterator it2 = str2.rbegin();
while (it2 != str2.rend())
{
cout << *it2 << " ";
//只读迭代不支持修改
//*it2 = '1';
++it2;
}
}
void test()
{
//范围for: 可以访问范围确定的序列
int arr[] = { 1, 2, 3, 4, 5 };
for (const auto& e : arr)
{
cout << e << " ";
}
cout << endl;
for (auto& e : arr)
{
e = 10;
}
string str = "12345";
//范围for:实际上是通过迭代器实现的
// 支持迭代器访问的自定义类型都可以支持范围for
for (auto& ch : str)
{
cout << ch << " ";
ch = 'a';
}
}
void test()
{
string str = "12345";
for (size_t i = 0; i < str.size(); ++i)
{
cout << str[i] << endl;
str[i] = 'a';
}
cout << endl;
}
void test()
{
string str;
cout << str.size() << endl;
//resize:修改有效字符的个数
//resize(n):如果有效字符的个数增加则新增的位置填充'\0'
//resize(n,ch):如果有效字符的个数增加,则新增的位置填充ch
//如果zize减小,不会进行填充
str.resize(10);
cout << str.size() << endl;
string str2 = "123";
cout << str2.size() << endl;
str2.resize(10);
cout << str2.size() << endl;
str2.resize(2);
cout << str2.size() << endl;
str2.resize(5, 'a'); //12aaa 5为有效位数,a为填充
str2.resize(2, 'b'); //12
}
void test()
{
string str = "123";
int sz = str.size();
//capacity:当前string中最多可以存放的元素个数
int cap = str.capacity();
str.resize(1);
sz = str.size();
cap = str.capacity();
str.resize(20);
//resize:当调整之后的size大于容量时,容量也会发生变化
sz = str.size();
cap = str.capacity();
//reserve:修改容量
//不影响size
str.reserve(60);
sz = str.size();
cap = str.capacity();
str.reserve(10);
sz = str.size();
cap = str.capacity();
//clear:清空有效字符
str.clear();
cap = str.capacity();
}
void test()
{
string str = "123";
int sz = str.size();
int cap = str.capacity();
str.shrink_to_fit();
sz = str.size();
cap = str.capacity();
str.reserve(100);
str.shrink_to_fit();
sz = str.size();
cap = str.capacity();
str.reserve(1000);
str.shrink_to_fit();
sz = str.size();
cap = str.capacity();
str.reserve(10000);
str.shrink_to_fit();
sz = str.size();
cap = str.capacity();
}
void test()
{
//增容规则:1.5倍
string str;
//提前开好空间,避免频繁的增容,提高代码的效率
str.reserve(200);
int cap = str.capacity();
cout << cap << endl;
for (int i = 0; i < 200; ++i)
{
str.push_back('a');
if (cap != str.capacity())
{
cap = str.capacity();
cout << cap << endl;
}
}
}
void test()
{
string str;
string str2 = "123";
str += str2; //123
str += "abc"; //123abc
str += '4'; //123abc4
str.operator+=('5'); //123abc45
}
void test()
{
string str;
string str2 = "123";
/*string(1)
string& append(const string& str);*/
str.append(str2);//123
/*substring(2)
string& append(const string& str, size_t subpos, size_t sublen);*/
str.append(str2, 1, 1);//1232
/*c - string(3)
string& append(const char* s);*/
str.append("abc");
/*buffer(4)
string& append(const char* s, size_t n);*/
str.append("123456", 6);//123abc123456
/*fill(5)
string& append(size_t n, char c);*/
str.append(5, 'm'); //123abc12345mmmmm
/*range(6)
template <class InputIterator>
string& append(InputIterator first, InputIterator last);*/
char arr[] = "abcde";
str.append(arr, arr + sizeof(arr) / sizeof(arr[0]));//12345abc123456mmmmmabcdefg
str.append(str2.begin(), str.end());//12345abc123456mmmmmabcdefg123
}
void test()
{
/*string(1)
string& assign(const string& str);
substring(2)
string& assign(const string& str, size_t subpos, size_t sublen);
c - string(3)
string& assign(const char* s);
buffer(4)
string& assign(const char* s, size_t n);
fill(5)
string& assign(size_t n, char c);
range(6)
template <class InputIterator>
string& assign(InputIterator first, InputIterator last);*/
string str;
string str2 = "123";
str.assign(str2);//123
str.assign(str2, 1, 1);//2
str.assign("abc");//abc
str.assign("abcde", 2);//abc
str.assign(5, 'a');//aaaaa
str.assign(++str2.begin(), str2.end());//23
}
void test()
{
/*string(1)
string& insert(size_t pos, const string& str);*/
string str = "123";
string str2 = "abc";
str.insert(0, str);//abc123
/*substring(2)
string& insert(size_t pos, const string& str, size_t subpos, size_t sublen);*/
str.insert(4, str2, 1, 2);//abc1bc23
/*c - string(3)
string& insert(size_t pos, const char* s);*/
str.insert(str.size(), "abc");//abc1bc23abc
/*buffer(4)
string& insert(size_t pos, const char* s, size_t n);*/
str.insert(5, "12345", 4); //abc1b 1234 c23abc
/*fill(5)
string& insert(size_t pos, size_t n, char c);
void insert(iterator p, size_t n, char c);*/
str.insert(str.begin(), 2, '0'); //00 abc1b1234c23abc
/*single character(6)
iterator insert(iterator p, char c);*/
/*range(7)
template <class InputIterator>
void insert(iterator p, InputIterator first, InputIterator last);*/
str.insert(str.end(), str2.begin(), str2.end());//00 abc1b1234c23abc123
}