数组
- 顺序存储
- 读操作多、写操作少
std::array
- 具有固定大小的数组,构造需指定大小
- 支持快速随机访问
- 不能添加或删除元素
#include <iostream>
#include <array>
#include <algorithm>
using namespace std;
int main(){
array<int, 5> arr{1, 2, 3, 5, 4};
array<int, 5> arr1 = arr;
array<int, 5> arr2(arr);
array<int, 5> arr3(std::move(arr));
cout << arr[2] << endl; // 运算符[]不会对索引值进行检查,arr[-1]是不会报错的
cout << arr.at(2) << endl; // at()将在运行期间捕获非法索引的,默认将程序中断
for(auto i = arr1.begin(); i != arr1.end(); ++i)
cout << *i << endl;
for(auto& a: arr2)
cout << a << endl;
for(auto i = arr3.rbegin(); i != arr3.rend(); ++i) // 反向遍历
cout << *i << endl;
std::cout << "empty(): " << arr.empty() << ", size(): " << arr.size() << "\n";
std::sort(arr1.begin(), arr1.end(), std::greater<int>());
for(const auto& a: arr1)
cout << a << endl;
std::sort(arr1.begin(), arr1.end(), std::less<int>());
for(const auto& a: arr1)
cout << a << endl;
}
std::vector
- 可变大小数组
http://t.csdn.cn/Yh677
#include <iostream>
#include <vector>
using namespace std;
int main(){
// 构造
vector<int> vec;
vector<int> vec1(5); // 大小为5
vector<int> vec2(5, 1); // 大小为5,初值为1
vector<int> vec3{5, 1}; // 初始化列表
// 访问
cout << vec1[0] << " " << vec2[0] << " "<< vec3[0] << " " << vec3.at(0) <<"\n";
// 大小与容量
vec.resize(10);
cout << vec[0] << endl;
cout << vec.size() << " " << vec.capacity() << endl;
vec.reserve(100); // 如果有大量的数据需要进行push_back,应当使用reserve()函数提前设定其容量大小,否则会出现许多次容量扩充操作
cout << vec.size() << " " << vec.capacity() << endl;
// 遍历
for(auto& v: vec)
cout << v << " ";
cout << "\n";
// 追加元素 emplace_back,比push_back少一次构造开销,优先使用,
for(int i=6; i<9; ++i){
vec.emplace_back(i); // 尾部追加
}
for(auto i = vec.begin(); i!= vec.end(); ++i)
cout << *i << " ";
cout << "\n";
// 追加元素 push_back
for(int i=0; i<5; ++i){
vec.push_back(i); // 尾部追加
}
for(auto i = vec.begin(); i!= vec.end(); ++i)
cout << *i << " ";
cout << "\n";
// 插入元素 emplace, 比insert少一次构造开销,优先使用,http://c.biancheng.net/view/6834.html
vec.emplace(vec.begin() + 2, 9);
cout << vec[2] << endl;
// 插入元素 insert
cout << vec[2] << endl;
vec.insert(vec.begin() + 2, 5);
cout << vec[2] << endl;
vec.insert(vec.begin() + 2, 5, 6); // 5个6
for(auto& v: vec)
cout << v << " ";
cout << "\n";
vec.insert(vec.begin() + 2, vec3.rbegin(), vec3.rend());
for(auto& v: vec)
cout << v << " ";
cout << "\n";
vec.insert(vec.begin() + 2, {1, 2});
for(auto& v: vec)
cout << v << " ";
cout << "\n";
// 删除元素
cout << vec.back() << endl;
vec.pop_back(); // 删除尾部元素
cout << vec.back() << endl;
vec.erase(vec.begin() + 2); // 删除第2个
for(auto& v: vec)
cout << v << " ";
cout << "\n";
vec.erase(vec.begin(), vec.begin() + 2); //删除前2个
for(auto& v: vec)
cout << v << " ";
cout << "\n";
}
散列表
unordered_map
https://www.cnblogs.com/langyao/p/8823092.html
unordered_map与map的区别 https://blog.csdn.net/jpc20144055069/article/details/108170073
#include <iostream>
#include <unordered_map>
using namespace std;
int main(){
// 初始化
unordered_map<int, int> hash;
unordered_map<int, int> hash1({{1, 9}, {2,99}});
unordered_map<int, int> hash2(hash1);
unordered_map<int, int> hash3(hash1.begin(), hash1.end());
unordered_map<int, int> hash4 = {{1, 9}, {2,99}};
cout << hash1.empty() << " " << hash1.size() << endl;
// 访问
cout << hash1[1] << endl;
cout << hash1.at(2) << endl; // 提供边界判断
// 查找
auto iter = hash1.find(2); // 返回迭代器,如果不存在key则段错误
cout << iter->first << " " << iter->second << endl;
cout << hash1.count(2) << endl; // .count返回是否存在key
// 遍历
for(auto it = hash1.begin(); it != hash1.end(); it++){
cout << it->first << " " << it->second << endl;
}
for(auto& it: hash1){
cout << it.first << " " << it.second << endl;
}
//插入
hash1[7] = 666;
hash1.emplace(3, 999); //效率比 insert() 方法高
for(auto& it: hash1){
cout << it.first << " " << it.second << endl;
}
hash1.insert({4, 9999});
hash1.insert({{4, 9999}, {1, 77}}); // key重复时,不插入
hash1.insert(hash2.begin(), hash2.end());
hash1.insert(pair<int, int>{6, 8888});
for(auto& it: hash1){
cout << it.first << " " << it.second << endl;
}
// 删除
hash1.erase(7);
hash1.erase(hash1.begin());
for(auto& it: hash1){
cout << it.first << " " << it.second << endl;
}
hash1.erase(hash1.begin(), hash1.end());
cout << hash1.empty() << " " << hash1.size() << endl;
}
栈
std::stack
http://c.biancheng.net/view/478.html
队列
std::queue
http://c.biancheng.net/view/479.html
字符串
std::string
https://www.cnblogs.com/X-Do-Better/p/8628492.html