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中,以換行符'\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類的查找函數

//查找成功時返回所在位置,失敗返回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實現


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM