1、vector——变长数组
倍增的思想
计算机申请空间的特点:花费时间与申请的大小无关,与申请速度有关(也就是和申请次数有关:申请1000个长度为1的空间花费时间可能是申请1个长度为1000的空间花费时间的1000倍)
//基本函数
vector<int> b;
b.size();//返回元素个数
b.empty();//判空
b.claer();//清空
front()/back(); //
push_back()/pop_back();
begin()/end();
[]支持随机存储
支持比较运算(按字典序比较)
#include <iostream>
#include <cstdio>
#include <csting>
#include <vector>
using namespace std;
int main()
{
//初始化
vector<int> b(10, 3);
//遍历方式1
for(auto k:b) cout << k << endl;
//用法
vector<int> a;
for(int i = 0; i < 10; i++) a.push_back(i);
//遍历方式2
for(int i = 0; i < a.size(); i++) cout << a[i] << ' ';
cout << endl;
//遍历方式3
for(vactor<int>::iterator i = a.begin(); i != a.end(); i++) cout << *i << ' ';
cout << endl;
}
pair<T, T > —— 定义二元组
pair<T, pair<T, T>> ——定义三元组
//基本用法
first
second
支持比较(以first为第一关键字,以second为第二关键字,字典序)
pair<int, string> a;
//两种
a = make_pair(10, "abc");
a = {10, "abc"};
2、string——字符串
#include <iostream>
#include <string>
using namespace std;
int main ()
{
std::string s0 ("Hello World"); //根据已有字符串构造新的string实例
//字符串长度
cout << "字符串长度:" << endl;
std::string s("1234567");
std::cout << "size=" << s.size() << std::endl;
std::cout << "length=" << s.length() << std::endl; //size()和length():返回string对象的字符个数,他们执行效果相同。
if(s[8] == '