目录
首先我们给出C++帮助文档网址,我们将根据帮助文档中的内容来进行学习
ASCII,gbk(中国自己造的贴合于汉字的编码表),unicode,utf-8,utf-16,utf-32这些都是编码表
编码表是 内存中实际存储的值 与 符号表 之间的一种映射
首先,要用string类,得引头文件#include<string>
string类模板的声明:
typedef basic_string<char> string;
1.是一个名字为basic_string的类模板
2.是一个动态增长的char类型的字符数组(顺序表).
一.默认成员函数
(一).constructor(构造函数)
#include<iostream>
#include<string>
using namespace std;
void test_string1()
{
//1.string();无参构造
string s1;
cout << s1 << endl;
//2.string (const char* s);有参构造
string s2("hello");
cout << s2 << endl;
//cin >> s1 >> s2;
//cout << s1 << endl;
//cout << s2 << endl;
//3.string(const string& str); 拷贝构造
string s3("hello world");
string s4(s3);
cout << s4 << endl;
}
void test_string2()
{
string s1("hello world");
//string (const string& str,
//size_t pos, size_t len = npos);
//把s1中的字符串从第五个字符开始拷贝给s2
string s2(s1, 5, 4);
cout << s2 << endl;
//" wor"
//pos位置后面的有效字符不够长,有多长拷贝多长
string s3(s1, 5, 24);
cout << s3 << endl;
//" world"
//size_t len是一个缺省参数
//注意:static const size_t npos = -1,
//-1的补码如果给无符号整型就是一个非常大的数
//同上,有多长拷多长
string s4(s1, 5);
cout << s4 << endl;
//" world"
}
void test_string3()
{
//string (const char* s, size_t n);
//只用前五个字符去初始化对象
string s1("hello world", 5);
cout << s1 << endl;
//"hello"
}
void test_string4()
{
//string(size_t n, char c);
//用40个o来初始化对象s1
string s1(40, 'o');
cout << s1 << endl;
}
void test_string5()
{
string s1 = "hello world!";
//也可以这样直接构造,先构造一个临时对象,再拷贝给s1
//但一般编译器经过优化后就相当于直接调用构造函数
//string s1 = "hello world!";
//<==>string s1("hello world");
}
(二).destructor(析构函数)
1.析构函数,会被自动调用.
2.为什么要有析构函数?
因为想要扩容,只有写在堆上才方便扩容,而堆空间必须依靠程序员手动释放,但有时会程序员会忘记释放堆空间,造成内存泄漏,因此需要析构函数来自动完成释放堆空间的工作.
(三).operator=(赋值运算符重载)
void test_string6()
{
string s1;
string s2 = "hello world";//构造+拷贝构造=>优化--直接构造
s1 = s2;//赋值,因为s1是已经存在的对象
s1 = "xxxx";
s1 = 'y';
}
注意区别=(是赋值运算符重载 还是 构造函数)
二.Element access(元素访问)
(一).operator[]
1.运用
-----------------------------------------------------------------------------------------------------------------------------
2.实现机理
得益于C++语法优势,用[]实际上是调用函数operator[]()来访问字符串数组
void test_string7()
{
string s1("hello");
s1[0] = 'x';
//s1.operator[](0)
//char& operator[](size_t pos)
//{
// assert(pos < _size);
// return _str[pos]
//}
//s1[7] = 'y';//会报错,上面的断言在起作用
}
(二).at
(三).back与front
三.Iterators(迭代器)
(一).迭代器
1.迭代器用法
void test_string1()
{
string s("hello");
//迭代器用法
string::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
++it;
}
cout << endl;
/*
h e l l o
请按任意键继续. . .
*/
}
2.注意事项
1.
iterator类似于是string的内部类
调用begin()函数,这个函数会返回第一个位置的迭代器(第一个指针(数组中第一个元素的地址))
string::iterator it = s.begin(); <==> char* it = s.begin()
调用end()函数,这个函数会返回最后一个位置下一个位置的迭代器
在这里,it是一个像指针(地址)一样的东西
2.
string::iterator
vector<int>::iterator
list<int>::iterator
迭代器都是在类域里的
3.
iterator是像指针一样的类型(有可能是指针,有可能不是指针,但是它的用法和指针一样)
3.为什么有了[]访问数组,还需要用迭代器呢?
string不喜欢用iterator,因为[]更好用
vector不喜欢用iterator,因为[]更好用 []适用于数组这样连续的空间
但是list链表,map,set..,这样的只能用迭代器访问,不适合用[]
iterator是所有容器的通用访问形式
#include<list> void test_string2() { list<int> lt(10, 1); list<int>::iterator lit = lt.begin(); while (lit != lt.end()) { cout << *lit << " "; ++lit; } cout << endl; }
list是带头循环链表,end()是最后一个字符的下一个,即就是哨兵位
4.范围for
(1).范围for的底层其实就是迭代器
#include<string>
#include<list>
//范围for的底层用的是迭代器
void test_string3()
{
string s("hello");
//范围for -- 自动迭代,自动判断结束
//依次取s中每个字符,赋值给ch
for (auto ch : s)
{
cout << ch << " ";
}
cout << endl;
list<int> lt(10, 1);
for (auto e : lt)//e element
{
cout << e << " ";
}
//范围for的底层其实是迭代器,就是把迭代器lt的值给了e
//迭代器要不要加加,要,要不要判断边界,要
cout << endl;
}
(2).利用范围for要想改变源字符串的值得用引用
void test_string4()
{
string s("hello");
for (auto ch : s)
{
ch++;
cout << ch << " ";
}
cout << endl;
cout << s << endl;
for (auto& ch : s)//用引用才能改变原串s里面的值
{
ch++;
cout << ch << " ";
}
cout << endl;
cout << s << endl;
}
(二).反向迭代器
void test_string5() { string s("hello"); string::reverse_iterator rit = s.rbegin(); while (rit != s.rend()) { cout << *rit << " "; ++rit;//反向迭代器++是从后向前走,++rit从后向前走 } cout << endl; /* o l l e h 请按任意键继续. . . */ }
s.rbegin()指向字符串最后一个元素,rit从后向前走
反向迭代器++朝左走,--朝右走
(三).const迭代器
注意,C++提供了const的迭代器,cbegin()与cend(),但是这是之后才补充上来的
当是普通对象时begin()与end()中的第一个函数会被调用.
当是const对象时begin()与end()中的第二个const函数会被调用.
我们一般习惯用begin()与end()
//写一个函数,打印出来str的内容.
//可以用[]与迭代器
void printString(const string& str)
{
//1.错误示例
//string::iterator it = str.begin();
//错,str是const对象,因此begin()返回的是const迭代器,const迭代器不能给普通迭代器
//2.const迭代器像是const指针一样,只能读,不能写
string::const_iterator it = str.begin();
//3.auto
//auto it = str.begin();
//auto不需要说明返回类型,begin()返回什么,就是什么
while (it != str.end())
{
//*it = 'x';//不能写
cout << *it << " ";//可以读
++it;
}
cout << endl;
}
void test_string6()
{
string s("hello");
printString(s);
}
(四).const反向迭代器
void reversePrintString(const string& str)
{
string::const_reverse_iterator rit = str.rbegin();
//auto rit = str.rbegin();
while (rit != str.rend())
{
//*it = 'x';//不能写
cout << *rit << " ";//可以读
++rit;
}
}
void test_string6()
{
string s("hello");
printString(s);
}
(五).其余几个迭代器
这几个迭代器是为了规范命名C++11才出现的, 但是之前C++中已经有const迭代器与const反向迭代器了, 我们知道后面更新的规则只能向上兼容, 因此之前的访问方式还可以用, 并且我们已经习惯了之前访问的那种形式, 因此这几个不常用,可以直接用begin,end,rbegin,rend,会自动识别普通对象与const对象.
四.访问string中元素的三种方式
访问string中元素有迭代器方式,[]方式,范围for方式, 用[]访问最方便, 但迭代器是所有容器通用的访问形式
(一).四种迭代器类型的比较
iterator
iterator begin(); iterator end();
const_iterator
const_iterator begin() const; const_iterator end() const;
reverse_iterator
reverse_iterator rbegin(); reverse_iterator rend();
const_reverse_iterator
const_reverse_iterator rbegin() const; const_reverse_iterator rend() const;
(二).[]
(三).范围for
只能正向遍历,不能反向遍历
void test_string8()
{
list<int> lt(10, 1);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
五.Modifiers
(一).operator+=
注意:+=的底层就是调用push_back和append
有了+=就不用push_back与append了
void test_string10()
{
string s("hello ");
string str("我最帅");
s += '&';//+=字符
cout << s << endl;
s += str;//+=string对象
cout << s << endl;
s += "!!!";//+=字符串
cout << s << endl;
}
运行结果:
hello &
hello &我最帅
hello &我最帅!!!
(二).push_back
void push_back(char c);//可以向字符串中插入字符
void test_string9()
{
string s("hello");
s.push_back('-');
s.push_back('-');
s.push_back('-');
s.append("world");
cout << s << endl;
}
(三).append(追加)
void test_string11()
{
string str1("我是大帅哥-");
str1 += str1;
cout << str1 << endl;
//append可以在后面追加
//在"我是大帅哥"后面追加一个"我是大帅哥"
//但是+=运算符重载也可以实现
string str2("我是大帅哥-");
str2.append(str2.begin(), str2.end());
cout << str2 << endl;
//但是要实现我是大帅哥++字符~我是大帅哥--字符,就必须append才能实现
string str3("我是大帅哥-");
str3.append(++str3.begin(), --str3.end());//在str3后面追加
cout << str3 << endl;
}
(四).用字符串区间来初始化对象
可以用迭代器控制字符串的一部分来初始化新的字符串
void test_string12()
{
string s1("hello");
string s2(++s1.begin(), --s1.end());//用迭代器(字符串区间)来初始化字符串s2
cout << s2 << endl;//ell
string s3(s1.begin() + 2, s1.end() - 1);//用迭代器(字符串区间)来初始化字符串s3
cout << s3 << endl;//ll
}
六.Capacity
(一).size与length
size与length都可以求字符串长度
但是在后面求树的结点时,size()更合适一点
(二).max_size与capacity
void test_string13()
{
string s1;
string s2("111111111111111");
cout << s1.max_size() << endl;
cout << s2.max_size() << endl;
cout << s2.capacity() << endl;
cout << s2.capacity() << endl;
/*
4294967294 这个类型的最大容量
4294967294
15 现阶段容量
15
请按任意键继续. . .
*/
}
(三).观察string中扩容情况
//i不断++,只要capacity改变,就打印出来capacity
void test_string14()
{
string s;
size_t sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s.push_back('c');
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
不要轻易使用C++底层的东西(某个功能函数具体实现过程),因为C++语法只是规定了要实现某个功能,但是具体怎么实现,没有规定,不同的厂家实现方法不一样.
(四).reserve与resize
注意区分reserve与reverse(逆置)
string s;
//s.reserve(1000);//提前开辟1000的空间
//s.resize(1000, 'x');//开空间+初始化 提前开1000的空间,不写'x'就初始化为全0
//i不断++,只要capacity改变,就打印出来capacity
void test_string14()
{
string s;
//s.reserve(1000);//提前开辟1000的空间
s.resize(1000, 'x');//开空间+初始化 提前开1000的空间,不写'x'就初始化为全0
size_t sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s.push_back('c');
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}