map是映射,储存的是键值对,后面两个都是储存一个值,vector是向量对象,相当于是对数组的封装,最后一个就是数组,最基本的类型,不属于类。 上面这几个,map访问和操作都是O(log2)级别的,后面两个访问和修改都是O(n)级别,
vector<map<int,vector<int>>> 从最外层看,这是个vector容器类型,可以存map<int,vector<int>>类型的元素。关于map<int,vector<int>>类型,见上面的解释。用法的话,和普通的map、vector一致,这是复合使用,大同小异。
n.push_back(<int,vector<int>>(1,m));//or n.insert(std::make_pair(1,m) m.erase(m.begin(),m.end);//or m.clear() if (!m.empty()){cout<<"error:"; m.push_back(2); m.push_back(5);m.push_back(9); n.push_back(<int,vector<int>>(2,m));//or n.insert(std::make...
1.map<string,int> m1; m1["def"] =2; 2. map<string,int> m2; m2.insert({"abc",1}); m2.insert(pair<string,int>(string("ghi"),3)); 3.map<string,int? m3 = {{"11",1},{"22",2},{"33",3}} 遍历: map<int, string>::iterator it; for (it = mapTemp.begin(); it !=...
vector<int> a(b); //用b向量来创建a向量,整体复制性赋值 vector<int> a(b.begin(),b.begin+3); //定义了a值为b中第0个到第2个(共3个)元素 int b[7]={1,2,3,4,5,9,8}; vector<int> a(b,b+7); //从数组中获得初值 2.基本操作 ...
bool map.empty() 若容器为空,则返回true,否则false it map.find(k) 寻找键值为k的元素,并用返回其地址 int map.size() 返回map中已存在元素的数量 map.insert({int,string}) 插入元素 for (itor = map.begin(); itor != map.end();)
下面的例子, 先输入一个字符串, 然后以空格分割装入<int, string>的map中。 然后循环遍历这个map,打印key和value(同上面的方法一,稍稍比较即可判断某个value是否存在); 然后把两个string写入vector中,然后判断string的值是否map中已存在(例子里面一个存在,另一个不存在),其中的判断,用到了...
数据类型长度、set、list、map、vector 32位编译器: char :1个字节 char*(即指针变量): 4个字节(32位的寻址空间是2^32) short int : 2个字节 int: 4个字节 unsigned int : 4个字节 float: 4个字节 double: 8个字节 long: 4个字节 long long: 8个字节...
//vector<int>::iterator 拿到vector<int>这种容器的迭代器类型 //第一种遍历方式 vector<int>::iterator pBegin = v.begin(); vector<int>::iterator pEnd = v.end(); while (pBegin != pEnd) { cout << *pBegin << endl; pBegin++;
classSolution{public:vector<int>singleNumber(vector<int>&nums){//使用图来实现哈希算法map<int,int>hash;//答案容器vector<int>ans;//遍历两次,干脆解决for(int i:nums){hash[i]++;}for(auto[num,cnt]:hash){if(cnt==1)ans.push_back(num);}returnans;}}; ...