deque
- 双端队列==支持迭代器
- 与vector差不多,区别是它多了可以对头的操作
push_front() 、 pop_front()
简述
deque容器为一个给定类型的元素进行线性处理,像向量一样,它能够快速地随机访问任一个元素,并且能够高效地插入和删除容器的尾部元素。但它又与vector不同,deque支持高效插入和删除容器的头部元素,因此也叫做双端队列。deque类常用的函数如下。
(1) 构造函数
deque():创建一个空deque
deque(int nSize):创建一个deque,元素个数为nSize
deque(int nSize,const T& t):创建一个deque,元素个数为nSize,且值均为t
deque(const deque &):复制构造函数
(2) 增加函数
void push_front(const T& x):双端队列头部增加一个元素X
void push_back(const T& x):双端队列尾部增加一个元素x
iterator insert(iterator it,const T& x):双端队列中某一元素前增加一个元素x
void insert(iterator it,int n,const T& x):双端队列中某一元素前增加n个相同的元素x
void insert(iterator it,const_iterator first,const_iteratorlast):双端队列中某一元素前插入另一个相同类型向量的[forst,last)间的数据
(3) 删除函数
Iterator erase(iterator it):删除双端队列中的某一个元素
Iterator erase(iterator first,iterator last):删除双端队列中[first,last)中的元素
void pop_front():删除双端队列中最前一个元素
void pop_back():删除双端队列中最后一个元素
void clear():清空双端队列中最后一个元素
(4) 遍历函数
reference at(int pos):返回pos位置元素的引用
reference front():返回手元素的引用
reference back():返回尾元素的引用
iterator begin():返回向量头指针,指向第一个元素
iterator end():返回指向向量中最后一个元素下一个元素的指针(不包含在向量中)
reverse_iterator rbegin():反向迭代器,指向最后一个元素
reverse_iterator rend():反向迭代器,指向第一个元素的前一个元素
(5) 判断函数
bool empty() const:向量是否为空,若true,则向量中无元素
(6) 大小函数
Int size() const:返回向量中元素的个数
int max_size() const:返回最大可允许的双端对了元素数量值
(7) 其他函数
void swap(deque&):交换两个同类型向量的数据
void assign(int n,const T& x):向量中第n个元素的值设置为x
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> deq;
deq.push_back(10);
deq.push_abck(20);
deq.push_abck(30);
cout<<"原始双端队列"<<endl;
for(int i=0;i<d.size();i++)
cout<<d.at(i)<<"\t";// at(int pos):返回pos位置元素的引用
cout<<endl;
d.push_front(5);
d.push_front(3);
d.push_front(1);
cout<<"after push_front(5.3.1):"<<endl;
for(int i=0;i<d.size();i++)
cout<<d.at(i)<<"\t";
cout<<endl;
d.pop_front();
d.pop_front();
cout<<"after pop_front() two times:"<<endl;
for(int i=0;i<d.size();i++)
cout<<d.at(i)<<"\t";
cout<<endl;
return 0;
}
vector
- 可以完全替代数组,非常好用
简述
vector a,b;
//b为向量,将b的0-2个元素赋值给向量a
***************************************************
a.assign(b.begin(),b.begin()+3);
//a含有4个值为2的元素
***************************************************
a.assign(4,2);
//返回a的最后一个元素
************************************************
a.back();
//返回a的第一个元素
***************************************************
a.front();
//返回a的第i元素,当且仅当a存在
***************************************************
a[i];
//清空a中的元素
*******************************************************
a.clear();
//判断a是否为空,空则返回true,非空则返回false
************************
a.empty();
//删除a向量的最后一个元素
**********************************************
a.pop_back();
//删除a中第一个(从第0个算起)到第二个元素,也就是说删除的元素从a.begin()+1算起(包括它)一直到a.begin()+3(不包括它)结束
***************************************************
a.erase(a.begin()+1,a.begin()+3);
//在a的最后一个向量后插入一个元素,其值为5
*******************************
a.push_back(5);
//在a的第一个元素(从第0个算起)位置插入数值5,
***************************************************
a.insert(a.begin()+1,5);
//在a的第一个元素(从第0个算起)位置插入3个数,其值都为5
***************************************************
a.insert(a.begin()+1,3,5);
//b为数组,在a的第一个元素(从第0个元素算起)的位置插入b的第三个元素到第5个元素(不包括b+6)
***************************************************
a.insert(a.begin()+1,b+3,b+6);
//返回a中元素的个数
***************************************************
a.size();
//返回a在内存中总共可以容纳的元素个数
***************************************************
a.capacity();
//将a的现有元素个数调整至10个,多则删,少则补,其值随机
***************************************************
a.resize(10);
//将a的现有元素个数调整至10个,多则删,少则补,其值为2
***************************************************
a.resize(10,2);
//将a的容量扩充至100,
***************************************************
a.reserve(100);
//b为向量,将a中的元素和b中的元素整体交换
***************************************************
a.swap(b);
//b为向量,向量的比较操作还有 != >= > <= <
***************************************************
a==b;
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void println(vector<int>& src)
{
vector<int>vec(src);
while(!vec.empty())
{
cout<<vec.back()<<"->";
vec.pop_back();
}
cout<<"\n\n";
}
int main()
{
/*
vector<int> vec(10,5);
vector<int>res;
res=vec;
cout<<res[3];
*/
/*
vector<int> vec(10,5);
vector<int>res(vec);
cout<<res[3];
*/
/*
vector<int> res(10);//一位数组
vector<vector<int>> res(10,vector<int>(10));//二维数组
vector<vector<int>> res(m,vector<int>(n));
*/
/*
vector<int> res{7,4,3,1,9,0,4,3,2};
vector<int> res={7,4,3,1,9,0,4,3,2};
*/
int arr[]={7,4,3,1,9,0,4,3,2};
vector<int> res(arr,arr+9);
println(res);
reverse(res.begin(),res.end());
println(res);
sort(res.begin(),res.end(),less<int>());//变小
println(res);
sort(res.begin(),res.end(),greater<int>());//变大
println(res);
return 0;
}
map
- 建立字符串和整数之间的映射
判断数据是否存在
字符串和字符串的映射 - map是一组键值对的组合,通俗理解类似一种特殊的数组,mp[key]=val,只不过数组元素的下标是任意一种类型,而且数组的元素的值也是任意一种类型。有点类似python中的字典。通过"键"来取值,类似生活中的字典,已知索引,来查看对应的信息。(个人理解,其实不准确,内部并不是数组实现的,而是红黑树)
// map的定义
*************************************************************
map<typename1,typename2> mp;
// 如果是字符串到其他类型的映射应该使用string
*************************************************************
map(string,int) mp;
// map的值也可以是stl容器
*************************************************************
map(set,string) mp;
// map容器内部的访问方式==通过下标来访问
*************************************************************
map<char,int> mp;
mp[‘c’]=10;
mp[‘c’]=20;//10被覆盖
cout<<mp[‘c’]<<endl;
// map容器内部的访问方式==通过迭代器来访问
// map的迭代器和其他集合的迭代器不同,其具有两种类型分别是
// typename1,typename2。
// 通过迭代器it要分别访问集合元素的键和值。
// it->first访问键,it->second访问值
*************************************************************
map<char,int> mp;
mp[‘a’]=10;
mp[‘b’]=20;
mp[‘c’]=30;
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
{
cout<first<<" "<second<<endl;
}
// map会以键从小到大自动排序,就是通过’a’,‘b’,'c’的顺序来排这三个键值对,这是由于集合内部是通过红黑树来实现的,在建立映射的时候会自动实现从小到大的排序
*************************************************************
// map中常见函数==find
// find(key)返回键为key的映射的迭代器,时间复杂度为O(logN),N为map重映射的数量。
*************************************************************
map<char,int> mp;
mp[‘a’]=10;
mp[‘b’]=20;
mp[‘c’]=30;
map<char,int>::iterator it=mp.find(‘a’);
cout<first<<’ '<second<<endl;
// map中常见函数==erase
// 删除单个元素
*************************************************************
map<char,int> mp;
mp[‘a’]=10;
mp[‘b’]=20;
mp[‘c’]=30;
map<char,int>::iterator it=mp.find(‘a’);
mp.erase(it);//删除a 10
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
{
cout<first<<" "<second<<endl;
}
// map中常见函数==erase
// 删除多个元素
*************************************************************
map<char,int> mp;
mp[‘a’]=10;
mp[‘b’]=20;
mp[‘c’]=30;
map<char,int>::iterator it=mp.find(‘a’);
mp.erase(it,mp.end());//删除it之后的所有映射
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
{
cout<first<<" "<second<<endl;
}
// map中常见函数==size
//
*************************************************************
map<char,int> mp;
mp[‘a’]=10;
mp[‘b’]=20;
mp[‘c’]=30;
cout<<mp.size()<<endl;
// 清空map中的所有元素
*************************************************************
map<char,int> mp;
mp[‘a’]=10;
mp[‘b’]=20;
mp[‘c’]=30;
mp.clear();//清空map
cout<<mp.size()<<endl;//0
```c
#include <iostream>
#include <map>
#include <pair>
#include <string>
using namespace std;
int main()
{
map<string,int> mapstringint;
map<int,string> mapintstring;
map<string,char>stringchar;
map<char,string>charstring;
map<char,int>charint;
map<int,char>intchar;
/*添加新元素*/
map<int,string>maplive;
maplive.insert(pair<int,string>(102,"aclive"));
maplive[112]="April";
maplive[113]="May";
maplive[114]="June";
cout<<"sdasda";
map<int,int> mp;
mp[1]=1;
mp[2]=2;
mp[3]=3;
//元素的查找=find()函数返回一个迭代器指向键值为key的元素,如果没有找到就返回map尾部的迭代器
map<int,string>::iterator 1_it;
1_it=maplive.find(112);
if(1_ir==mplive.end())
cout<<"we do not find 112"<<endl;
else
cout<<"we find 112"<<endl;
/*元素的删除*/
map<int,string>::iterator 2_it;
2_it=maplive.find(113);
if(2_it==maplive.find(113))
cout<<"we do not find 113"<<endl;
else
maplive.erase(2_it);
/*对应元素的键和值*/
map<int,string>::iterator w_it;
w_it=maplive.find(114);
if(w_it==maplive.end())
cout<<"we do not find 114"<<endl;
else
cout<<w_it->firdt<<endl;
}
pair==和map搭配
// 创建一个空的pair对象(使用默认构造),它的两个元素分别是T1和T2类型,采用值初始化。
*************************************************************
pair<T1, T2> p1;
// 创建一个pair对象,它的两个元素分别是T1和T2类型,其中first成员初始化为v1,second成员初始化为v2。
*************************************************************
pair<T1, T2> p1(v1, v2);
// 以v1和v2的值创建一个新的pair对象,其元素类型分别是v1和v2的类型。
*************************************************************
make_pair(v1, v2);
// 两个pair对象间的小于运算,其定义遵循字典次序:如 p1.first < p2.first 或者 !(p2.first < p1.first) && (p1.second < p2.second) 则返回true。
*************************************************************
p1 < p2;
// 如果两个对象的first和second依次相等,则这两个对象相等;该运算使用元素的==操作符。
*************************************************************
p1 == p2;
// 返回对象p1中名为first的公有数据成员
*************************************************************
// 返回对象p1中名为first的公有数据成员
*************************************************************
p1.first;
// 返回对象p1中名为second的公有数据成员
*************************************************************
p1.second;
// pair的创建和初始化:
// 创建一个空对象anon,两个元素类型都是string
*************************************************************
pair<string, string> anon;
// 创建一个空对象 word_count, 两个元素类型分别是string和int类型
*************************************************************
pair<string, int> word_count;
// 创建一个空对象line,两个元素类型分别是string和vector类型
定义时进行成员初始化:
*************************************************************
pair<string, vector > line;
// 创建一个author对象,两个元素类型分别为string类型,并默认初始值为James和Joy。
*************************************************************
pair<string, string> author(“James”,“Joy”);
*************************************************************
pair<string, int> name_age(“Tom”, 18);
// 拷贝构造初始化变量间赋值:
*************************************************************
pair<string, int> name_age2(name_age);
*************************************************************
pair<int, double> p1(1, 1.2);
pair<int, double> p2 = p1; // copy construction to initialize object
pair<int, double> p3;
p3 = p1; // operator =
/*******************************************************************
3,pair对象的操作
pair<int ,double> p1;
p1.first = 1;
p1.second = 2.5;
cout<<p1.first<<’ '<<p1.second<<endl;
//输出结果:1 2.5
**********************************************************************/
/********************************************************************
4、生成新的pair对象
pair<int, double> p1;
p1 = make_pair(1, 1.2);
cout << p1.first << p1.second << endl;//output: 1 1.2
int a = 8;
string m = “James”;
pair<int, string> newone;
newone = make_pair(a, m);
cout << newone.first << newone.second << endl;
*/
/
5、通过tie获取pair元素值
std::pair<std::string, int> getPreson() {
return std::make_pair(“Sven”, 25);
}
int main(int argc, char **argv) {
std::string name;
int ages;
std::tie(name, ages) = getPreson();
std::cout << "name: " << name << ", ages: " << ages << std::endl;
return 0;
}
*******************************************************************/
#include <iostream>
#include <pair>
using namespace std;
int main()
{
vector<int> a={5,12,34,77,90,11,2,4,5,55};
unordered_map<int,int> mp;
set<int> st;
mp[1]=1;
mp[2]=2;
mp[3]=3;
mp.insert(pair<int,int>(1,3));
st.insert(4);
st.insert(5);
st.insert(6);
vector<pair<int,int>> vec(mp.begin(),mp.end());
vector<int> vec1(st.begin(),st.end());
for(auto iter:mp)
cout<<iter.first<<";"<<iter.second<<endl;
for(auto iter:vec1)
cout<<iter<<endl;
return 0;
}
set
/****************************************************************************
set是一个集合(与map一样都是关联式容器),内部的元素是唯一的,且不允许修改,因为元素是一对一的
内部实现时RB树,尤其是插入和删除的效率高===由于实现的机制一样,导致了带有方法也是差不多
****************************************************************************/
//set的用法
begin() 返回set容器的第一个元素********************
end() 返回最后一个元素的位置(迭代器类型)*******************迭代器是一个模板类,表现的像指针
begin() 和 end()函数是不检查set是否为空的,使用前最好使用empty()检验一下set是否为空.
clear() 清空容器,删除所有元素*************************
empty() ******************************
max_size() 返回set容器的容量******************************************
size() ********************************************
rbegin() 返回的值和end()相同
rend() 返回的值和begin()相同
erase(2) 删除此元素*****************************
insert(2) 插入此元素*****************************
inset(first,second);将定位器first到second之间的元素插入到set中,返回值是void.
find() 返回给定值的定位器,如果没找到则返回end()。*******************************************
*/
#include <iostream>
#include <set>
using namepace std;
/*
int main()
{
int arr[]={1,2,3};
set<int> s(arr,arr+3);
s.insert(1);
cout<<*s,.end();
cout<<"元素1的个数"<<s.count(1)<<endl;//1
cout<<"set容器的容量"<<s.max_size()<<endl;
cout<<s.size()<<endl;
s,erase(1);
cout<<s.szie()<<endl;
set<int>::iterator iter;
if(iter=s.find(2)!=s.end())//在这个集合中,如果没有这个元素就返回end(),如果有就返回这个元素的位置的值
cout<<*iter<<endl;
return 0;
}
*/
/*
int main()
{
set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(1);
s.insert(1);
for (auto aa : s)
{
cout<<aa<<endl;
}
cout << "set 中 1 出现的次数是 :" << s.count(1) << endl;
cout << "set 中 4 出现的次数是 :" << s.count(2) << endl;
return 0;
}
*/
queue
/****************************************************
主要对队列的函数的使用======不支持迭代器
queue.empty(); 队列是否为空
queue.size(); 队列里的元素个数,队列的大小
queue.front(); 返回头部元素的值
queue.back(); 返回尾部元素的值
queue.push(a); 从尾巴加入一个元素a
queue.pop(); 从头部弹出一个元素
***************************************************/
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int> Que;
Que.push(NULL);
if(Que.empty())
cout<<"empty!";
cout<<"\n"<<Que.size()<<end;
for(int i=0;i<10;i++)
{
Que.push(i+1);
cout<<Que.back()<<"\t";
}
cout<<"\n"<<Que.size();
cout<<"\n";
for(int j=0;j<10;j++)
{
cout<<Que.front()<<"\t";
Que.pop();
}
return 0;
}
stack
/************************************************************
栈的应用:
函数的调用
逆波兰表达式求值======后缀表达式求值 ===主要利用栈
括号匹配
****************************************************************/
举例子:
//================================后缀表达式求值=逆波兰表达式求值
/*
从左向右扫描表达式
遇到数字就压入栈,
遇到操作符表示可以进行计算,取出栈顶的两个元素进行操作
然后再次将结果压入到栈
最后栈里会留下一个元素,==次元素就是最后的结果
*/
#include <iostream>
#include <stack>
#include <assert.h>
using namespace std;
enum Symbol{
Op_symbol,//======0
Op_num,//=======1
Add,
Sub,
Mul,
Div
};
struct cell {
int cur_symbol;//是op_num 或者是 op_symbol
int cur_num;
};
int main()
{
std::stack<int> Sta;
//=====存放数据的结构体数组-----9 6 5 + 2 * +
cell arry[] = {
{Op_num,9},
{Op_num,6},
{Op_num,5},
{Op_symbol,Add},
{Op_num,2},
{Op_symbol,Mul},
{Op_symbol,Add}
};
//开始从左向右扫描
int arr_len = sizeof(arry) / sizeof(arry[0]);
for (int i = 0; i <arr_len; i++)
{
//判断读取的是数字还是字母
if (arry[i].cur_symbol == 1)//是数字就压栈里
{
Sta.push(arry[i].cur_num);
}
else //就是符号,取栈顶上的两个元素,进行运算
{
int right = Sta.top();
Sta.pop();
int left = Sta.top();
Sta.pop();
switch (arry[i].cur_symbol)
{
case Add:
Sta.push(left+right);
break;
case Sub:
Sta.push(left - right);
break;
case Mul:
Sta.push(left * right);
break;
case Div:
Sta.push(left / right);
break;
//default:
//assert(false);//报错检测
//break;
}
}
}
cout<<"\n"<<Sta.top()<<"\n";
Sta.pop();
if (Sta.empty()) cout<<"是空的容器"<<endl;
system("pause");
string
/*
using std::string;
using std::wstring;
或
using namespace std;
下面你就可以使用string / wstring了,它们两分别对应着char和wchar_t。
string和wstring的用法是一样的,以下只用string作介绍:
string类的构造函数:
string(const char *s); //用c字符串s初始化
string(int n, char c); //用n个字符c初始化
此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2 = “hello”;都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常 ;
string类的字符操作:
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
const char *data()const;//返回一个非null终止的c字符数组
const char *c_str()const;//返回一个以null终止的c字符串
int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目
string的特性描述:
int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const; //返回string对象中可存放的最大字符串的长度
int size()const; //返回当前字符串的大小
int length()const; //返回当前字符串的长度
bool empty()const; //当前字符串是否为空
void resize(int len, char c);//把字符串当前大小置为len,并用字符c填充不足的部分
string类的输入输出操作:
string类重载运算符operator >> 用于输入,同样重载运算符operator << 用于输出操作。
函数getline(istream &in, string &s); 用于从输入流in中读取字符串到s中,以换行符’\n’分开。
string的赋值:
string &operator=(const string &s);//把字符串s赋给当前字符串
string &assign(const char *s);//用c类型字符串s赋值
string &assign(const char *s, int n);//用c字符串s开始的n个字符赋值
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n, char c);//用n个字符c赋值给当前字符串
string &assign(const string &s, int start, int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first, const_itertor last);//把first和last迭代器之间的部分赋给字符串
string的连接:
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾
string &append(const char *s); //把c类型字符串s连接到当前字符串结尾
string &append(const char *s, int n);//把c类型字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s); //同operator+=()
string &append(const string &s, int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(int n, char c); //在当前字符串结尾添加n个字符c
string &append(const_iterator first, const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾
string的比较:
bool operator==(const string &s1, const string &s2)const;//比较两个字符串是否相等
运算符">", “<”, “>=”, “<=”, "!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n, const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n, const string &s, int pos2, int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中
//pos2开始的n2个字符组成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n, const char *s) const;
int compare(int pos, int n, const char *s, int pos2) const;
compare函数在 > 时返回1, < 时返回 - 1, == 时返回0
string的子串:
string substr(int pos = 0, int n = npos) const;//返回pos开始的n个字符组成的字符串
string的交换:
void swap(string &s2); //交换当前字符串与s2的值
string类的查找函数:
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s, int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s, int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos, int n) const;
int find_first_not_of(const string &s, int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s, int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s, int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
string类的替换函数:
string &replace(int p0, int n0, const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0, const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0, const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0, const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0, int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0, const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0, const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0, const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0, int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0, const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串
string类的插入函数:
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0, const string &s);
string &insert(int p0, const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c
string类的删除函数
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串
string类的迭代器处理:
string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。
用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:
const_iterator begin()const;
iterator begin(); //返回string的起始位置
const_iterator end()const;
iterator end(); //返回string的最后一个字符后面的位置
const_iterator rbegin()const;
iterator rbegin(); //返回string的最后一个字符的位置
const_iterator rend()const;
iterator rend(); //返回string第一个字符位置的前面
rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator, string::const_reverse_iterator实现
字符串流处理:
通过定义ostringstream和istringstream变量实现,#include 头文件中
例如:
string input(“hello, this is a test”);
istringstream is(input);
string s1, s2, s3, s4;
is >> s1 >> s2 >> s3 >> s4;//s1=“hello,this”,s2=“is”,s3=“a”,s4=“test”
ostringstream os;
os << s1 << s2 << s3 << s4;
cout << os.str();
*/
参考链接:https://blog.csdn.net/longshengguoji/article/details/8519812
参考链接:https://www.cnblogs.com/mengxiaoleng/p/11375678.html
参考链接:
参考链接: