C++常用容器

vector

顺序容器,和数组类似,可从尾部快速的插入和删除,可随机访问。

vector的常用成员函数:

#include<vector>
std::vector<type> vec;
std::vector<type> vec(size);
std::vector<type> vec(size,value);
std::vector<type> vec(myvector);
std::vector<type> vec(first,last);

operators:==、!=、<=、>=、<、>、[]
push_back(x):把x推入(插入)到向量的尾部
pop_back():弹出(删除)向量最后一个元素
begin():返回向量中第一个元素的迭代器
end():返回向量中最后一个元素的下一个迭代器(仅作结束游标,不可解引用)
assign(first,last):用迭代器first,last所指定的元素取代向量元素
assign(num,val):用val的num份副本取代向量元素
at(n):等价于[]运算符,返回向量中位置n的元素,因其有越界检查,故比[]索引访问安全
front():返回向量中第一个元素的引用
back():返回向量中最后一个元素的引用
max_size():返回向量类型的最大容量(2^30-1=0x3FFFFFFF)
capacity():返回向量当前开辟的空间大小(<= max_size,与向量的动态内存分配策略相关)
size():返回向量中现有元素的个数(<=capacity)
clear():删除向量中所有元素
empty():如果向量为空,返回真
erase(start,end):删除迭代器start end所指定范围内的元素
erase(i):删除迭代器i所指向的元素,返回指向删除的最后一个元素的下一位置的迭代器
insert(i,x);把x插入到迭代器i所指定的位置之前
insert(i,n,x):把x的n份副本插入到迭代器i所指定的位置之前
insert(i,start,end):把迭代器start和end所指定的范围内的值插入到迭代器i所指定的位置之前
rbegin():返回一个反向迭代器,该迭代器指向的元素越过了向量中的最后一个元素
rend():返回一个反向迭代器,该迭代器指向向量中第一个元素
reverse():反转元素顺序
resize(n,x):把向量的大小改为n,新元素的初值赋为x
swap(vectorref):交换2个向量的内容

map

关联容器,基于关键字快速查找,不允许重复值

map常用的成员函数:

#include<map>
std::map<key, value> mp;
std::map<key, value, comp> mp;
	key为键值
	value为映射值
	comp可选,为键值对存放策略,例如可为std::less<>,键值映射对将按键值从小到大存储

count():返回map中键值等于key的元素的个数
find(key):返回键值为key的键值对迭代器,如果没有该映射则返回结束游标end()
begin():返回指向第一个元素的迭代器,通过iterator->first返回key,iterator->second访问value
end():返回指向最后一个元素后一个位置的迭代器,通过iterator->first返回key,iterator->second访问value
[]操作符:访问map中的key对应的value,注意map的[]操作符,当试图对于不存在的key进行引用时,将新建键值对,值为空。
equal_range():函数返回两个迭代器——一个指向第一个键值为key的元素,另一个指向最后一个键值为key的元素
erase(i):删除迭代器所指位置的元素(键值对)
lower_bound():返回一个迭代器,指向map中键值>=key的第一个元素
upper_bound():函数返回一个迭代器,指向map中键值>key的第一个元素

set

关联容器,一组元素的集合,元素值是唯一的,而且按照一定顺序排列。

set常用的成员函数:

#include <set>
std::set<key> set;

count():返回map中键值等于key的元素的个数
find(key):返回键值为key的键值对迭代器,如果没有该映射则返回结束游标end()
begin():返回指向第一个元素的迭代器
end():返回指向最后一个元素后一个位置的迭代器
insert():在set中插入元素
erase(i):删除迭代器所指位置的元素

stack

容器适配器,实现后进先出的操作,基于deque容器实现。

stack常用的成员函数:

#include<stack>
std::stack<type,container> stk;
	type为堆栈操作的数据类型
	container为实现堆栈所用的容器类型,默认基于deque,还可以为std::vector和std::list
	例如std::stack<int,std::list<int>> IntStack;

top():返回顶端元素的引用
push(x):将元素压入栈(顶)
pop():弹出(删除)顶端元素

queue

容器适配器,实现先入先出的操作,基于dequeue容器实现。

queue常用的成员函数:

#include<queue>
std::queue<type,container> que;
	type为队列操作的数据类型
	container为实现队列所用的容器类型,只能为提供了push_front操作的std::deque或std::list,默认基于std::deque

front():返回队首元素的引用
back():返回队尾元素的引用
push(x):把元素x推入(插入)到队尾
pop():队首元素出列(弹出(删除)队首元素)

pair

pair常用的成员函数:

pair<T1, T2>p1:创建一个空的pair对象,它的两个元素分别是T1和T2类型,采用值初始化
pair<T1, T2>,p1(v1, v2):创建一个pair对象,它的两个元素分别是T1和T2,其中first成员初始化为v1,而second成员初始化为v2
make_pair(v1,v2):以v1和v2值创建一个新pair对象,其元素类型分别是v1和v2的类型
p1 < p2:两个pair对象之间的小于运算,其定义遵循字典次序:如果 p1.first< p2.first或者!(p2.first<p1.first)&&p1.second<p2.second,则返回true
p1 == p2:如果两个pair对象的first和second成员依次相等,则这两个对象相等。该运算使用其元素的==操作符
p.first:返回p中名为first的(公有)数据成员
p.second:返回p的名为second的(公有)数据成员

例子:

//pairs类型定义和初始化
pair<string, string> test("A", "B");

//pairs对象的操作
string firstBook;
if (author.first == "James" && author.second == "Joyce")firstBook = "Stephen Hero";
//生成新的pair对象
pair<string, string> next_auth;
next_auth = make_pair("A","B");//第一种方法
next_auth = pair<string, string>("A","B"); //第二种方法
cin >> next_auth.first >> next_auth.second;//第三种方法

string

头文件

#include <string> //注意不带.h,带.h的是C语言中的头文件
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中,以换行符'
'分开。

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类的查找函数

//查找成功时返回所在位置,失败返回string::npos的值
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在当前串中的位置

//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回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;

//从当前串中查找第一个不在串s中的字符出现的位置,失败返回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;

//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
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;

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类的插入函数

//前4个函数在p0位置插入字符串s中pos开始的前n个字符
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);

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实现
原文地址:https://www.cnblogs.com/coderland/p/5902725.html