vector:
- push_back
- pop_back
- size()
1.push_back 在数组的最后添加一个数据
2.pop_back 去掉数组的最后一个数据
3.at 得到编号位置的数据
4.begin 得到数组头的指针
5.end 得到数组的最后一个单元+1的指针
6. front 得到数组头的引用
7.back 得到数组的最后一个单元的引用
8.max_size 得到vector最大可以是多大
9.capacity 当前vector分配的大小
10.size 当前使用数据的大小
11.resize 改变当前使用数据的大小,如果它比当前使用的大,者填充默认值
12.reserve 改变当前vecotr所分配空间的大小
13.erase 删除指针指向的数据项
14.clear 清空当前的vector
15.rbegin 将vector反转后的开始指针返回(其实就是原来的end-1)
16.rend 将vector反转构的结束指针返回(其实就是原来的begin-1)
17.empty 判断vector是否为空
18.swap 与另一个vector交换数据
#include <string.h>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int>obj;//创建一个向量存储容器 int
for(int i=0;i<10;i++) // push_back(elem)在数组最后添加数据
{
obj.push_back(i);
cout<<obj[i]<<",";
}
for(int i=0;i<5;i++)//去掉数组最后一个数据
{
obj.pop_back();
}
cout<<"\n"<<endl;
for(int i=0;i<obj.size();i++)//size()容器中实际数据个数
{
cout<<obj[i]<<",";
}
return 0;
}
stack:
- top():返回一个栈顶元素的引用,类型为 T&。如果栈为空,返回值未定义。
- push(const T& obj):可以将对象副本压入栈顶。这是通过调用底层容器的 push_back() 函数完成的。
- push(T&& obj):以移动对象的方式将对象压入栈顶。这是通过调用底层容器的有右值引用参数的 push_back() 函数完成的。
- pop():弹出栈顶元素。
- size():返回栈中元素的个数。
- empty():在栈中没有元素的情况下返回 true。
- emplace():用传入的参数调用构造函数,在栈顶生成对象。
swap(stack<T> & other_stack):将当前栈中的元素和参数中的元素交换。参数所包含元素的类型必须和当前栈的相同。对于 stack 对象有一个特例化的全局函数 swap() 可以使用。
queue
https://blog.csdn.net/mayue_web/article/details/86062742
#include<queue>
push(e):将元素e压入队列尾部
pop():将队列首部元素弹出,无返回
front():获取队列头部元素
back():获取队列尾部元素
empty():判断队列是否为空
size():取得队列的大小
hash
#include<unordered_map>
hash表用法,记得几个关键的,
引入
#include <unordered_map>
定义
unordered_map<int, string> unomap;
引入的时候可以定义初始化,例如
unordered_map<int,string> unomap ={{1,"dd"},{2,"ssss"}};
打印查找:
for(auto x: unomap){
cout<<unomap[x.first]<<endl;
cout<<x.second<<endl;//以value值输出
}
myMap.insert(pair<int, string>(3,"陈二"));//插值用pair的形式
myMap.insert(pair<int,string>(3,"陈二"));//以pair的形式插入
myMap[2] = "zhangsan";//类比python字典,有加替换,value,没有直接创建一个出来。
- emplace(key, value)
- find(key) -> iterator
An iterator to the element, if the specified key value is found, or unordered_map::end if the specified key is not found in the container.
Member types iterator and const_iterator are forward iterator types.
- count(key) -> size_type
- size()->size_type
- empty()->bool
// unordered_map::emplace
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap;
mymap.emplace ("NCC-1701", "J.T. Kirk");
mymap.emplace ("NCC-1701-D", "J.L. Picard");
mymap.emplace ("NCC-74656", "K. Janeway");
std::cout << "mymap contains:" << std::endl;
for (auto& x: mymap)
std::cout << x.first << ": " << x.second << std::endl;
std::cout << std::endl;
return 0;
}
// unordered_map::find
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,double> mymap = {
{"mom",5.4},
{"dad",6.1},
{"bro",5.9} };
std::string input;
std::cout << "who? ";
getline (std::cin,input);
std::unordered_map<std::string,double>::const_iterator got = mymap.find (input);
if ( got == mymap.end() )
std::cout << "not found";
else
std::cout << got->first << " is " << got->second;
std::cout << std::endl;
return 0;
}
// unordered_map::count
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,double> mymap = {
{"Burger",2.99},
{"Fries",1.99},
{"Soda",1.50} };
for (auto& x: {"Burger","Pizza","Salad","Soda"}) {
if (mymap.count(x)>0)
std::cout << "mymap has " << x << std::endl;
else
std::cout << "mymap has no " << x << std::endl;
}
return 0;
}