?
由于 C++ 的复杂和多样性, 因此为了方便查找和使用, 因此很多STL有重复的部分.建议在使用这些 STL 前先点击 STL 查看其简要描述, 然后再去查看 STL 中提供的方法
?
STL 中没出现的方法是因为这些方法不常用或无法使用 (例如 vector 不能用 .push_front() 等)
?
?
?
本文中由于一些 STL 和方法在 C++11 及更高标准中才能使用. 因此如果编译器支持的 C++ 标准过低 (如 vc6.0 等只支持 C++98 标准), 建议更换编译器 (建议使用 g++, clang, visual studio 2013及更高版本, dev - cpp等)
?
?
容器分为顺序容器与关联容器, 本文从<vector>到<pair>前为顺序容器, <pair>到<set>为关联容器. 主要区别在于关联容器通过关键词来访问元素, 而顺序容器通过位置访问元素.
?
?
?
vector 是一种顺序容器, 大致可看成一种可变大小的数组, 优点是访问速度快, 但是在尾部之外插入或删除元素速度较慢. 支持大部分操作, 不能在首元素前插入元素
?
关于多维vector请点击 : https://www.cnblogs.com/ICeVe/p/14354926.html
?
?
?
用途 : 查看vector是否为空. 如果为空返回 0, 否则为 1
一般形式 :
vector<typename>V.empty();
/*typename意为类型名, 其中包括顺序容器和数据类型. V为自定义的标识符*/
?
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
vector<int> V1;
vector<int> V2{1, 2, 3};
cout << V1.empty() << endl;
cout << V2.empty() << endl;
return 0;
}
?
输出结果
1
0
?
?
?
?
用途 : 求vector中有多少个元素的.
一般形式 :
vector<typename>V.size();
/*typename意为类型名, 其中包括顺序容器和数据类型. V为自定义的标识符*/
?
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
vector<int> V{1, 2, 3};
cout << V.size() << endl;
return 0;
}
?
输出结果
3
?
?
?
?
用途 : 求vector能容纳的最大元素数量
一般形式 :
vector<typename>V.max_size();
/*typename意为类型名, 其中包括顺序容器和数据类型. V为自定义的标识符*/
?
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
vector<int> V;
cout << V.max_size() << endl;
return 0;
}
?
输出结果
4611686018427387903
?
?
?
?
用途 : 在vector尾部插入元素
一般形式 :
vector<typename>V.pushback(x);
/*typename意为类型名, 其中包括顺序容器和数据类型. V为自定义的标识符, x为变量或常量*/
?
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
vector<int> V;
V.push_back(1);
cout << V[0] << endl; /*vector可用下标访问*/
return 0;
}
?
输出结果
1
?
?
?
?
用途 : 在vector中某一处插入元素.
一般形式 :
vector<typename>V.insert(iterator, x);
/*typename意为类型名, 其中包括顺序容器和数据类型. V为自定义的标识符, iterator为迭代器, x为变量或常量*/
?
原文:https://www.cnblogs.com/ICeVe/p/14526492.html