c++11之STL容器
map
map<string,string> testsxz;
//插入
testsxz.insert(pair<string, string>("he","456"));
//修改
testsxz["Kobe"] = "100";//遍历
for(auto iter = testsxz.begin(); iter != testsxz.end(); iter++)cout<<iter->first<<" "<<iter->second<<"\n";//逆遍历
for(auto iter = testsxz.rbegin(); iter != testsxz.rend(); iter++)cout<<iter->first<<" "<<iter->second<<"\n";//decltype用法
std::map<int, std::string> mis;
typedef decltype(std::map<int, std::string>::value_type()) misType;
mis.insert(misType(1, "php"));
vector
//在第二个位置插入新元素
v5.insert(v5.begin()+1,9);
//连续插入4个100
v5.insert(v5.begin() + 1, 4,100); //迭代器遍历
for ( vector<int>::iterator it = v5.begin(); it != v5.end(); it++)cout << *it << endl;//数组遍历
for (int i = 0; i < s3.size(); i++)cout << v5[i] << endl;#include <algorithm>
std::for_each(vec.begin(), vec.end(), [&](char* a){cout << a << endl;
});
//img 转 vector
QImage img;
img.load("o1.jpg");
img.save("222.jpg");
QByteArray imgdata = QImage2Base64(img, "jpg");
std::vector<unsigned char> bufff(imgdata.data(), imgdata.data()+ imgdata.length());
sendImgData.data.swap(bufff);
// vector 转 img
QByteArray bytes((char*)&nsImgData.data[0], nsImgData.data.size());
QImage img;
img.loadFromData(bytes);
img.save("333.jpg");
showLableImg(globalui->label, img);
vector二维
//声明
std::vector<std::vector> sss;
//申请内存
int rowLen = 10;
int colLen = 3;
std::vector<std::vector> aaa(rowLen, std::vector(colLen));
sss.swap(aaa);
//深度拷贝
std::vector<std::vector> qqq;
//qqq = sss;
qqq.assign(sss.begin(), sss.end());
//清空
std::vector<std::vector> bbb;
//sss.swap(bbb);
unordered_set
struct myHash
{size_t operator()(std::pair<int, std::string> __val) const{return static_cast<size_t>(__val.first * __val.first);}
};std::unordered_set<std::pair<int, std::string>,myHash> pairSetMyHash;
pair
std::pair<int, std::string> paire1(4,“444”);
paire1 = std::pair<int, std::string>(1, “1111”);
std::cout << paire1.first << " " << paire1.second << " \n";
paire1 = std::make_pair(2, “222”);
std::cout << paire1.first << " " << paire1.second << " \n";
std::set<std::pair<int, std::string>> pairSetTest;
for (int i = 0; i < 3; i++)
{
pairSetTest.insert(std::pair<int, std::string>(i ,std::to_string(i*i)));
}
for (auto iter= pairSetTest.begin(); iter!= pairSetTest.end(); iter++)
{
std::cout << (*iter).first << " " << (*iter).second << " \n";
}
std::cout << “--------------------------for_each test ---------------------\n”;
std::for_each(pairSetTest.begin(), pairSetTest.end(), [&](std::pair<int, std::string> paire) {
std::cout << paire.first << " " << paire.second << " \n";
});
std::cout << “--------------------------new for test ---------------------\n”;
for (auto paire: pairSetTest)
{
std::cout << paire.first << " " << paire.second << " \n";
}
tuple
std::tuple<int, int, int> pos(10, 11, 12);std::cout << std::get<0>(pos) << " "<<std::get<1>(pos) << " "<<std::get<2>(pos) << " \n";pos = std::make_tuple(100, 110, 120);std::cout << std::get<0>(pos) << " " << std::get<1>(pos) << " " << std::get<2>(pos) << " \n";std::cout <<"tuple_size "<< std::tuple_size<decltype(pos)>::value<<"\n";int x=-1, y=-1, z=-1;std::tie(x, y, std::ignore) = pos;std::cout << x << " " << y << " " <<z<< " \n";std::vector<std::tuple<int, float, double> > tupVec;for (int i=0;i<3;i++)tupVec.push_back(std::tuple<int, int, int>(i * 1, i * 10, i * 100));for (int i = 0; i < 3; i++)std::cout<<std::get<0>(tupVec[i]) <<" "<< std::get<1>(tupVec[i]) << " " << std::get<2>(tupVec[i]) << "\n";
回调函数
typedef int(*FUNP1)(int a,int b);
void fun1(FUNP1 fup,int m,int n)
{int ret=(*fup)(m,n);std::cout << __FUNCTION__ << " a+b " << ret << "\n";
}
typedef void(*FUNP2)();
void fun2(FUNP2 fup)
{(*fup)();
}//demoint callbackFunc1(int a,int b) {std::cout << __FUNCTION__ <<" "<<a<< " "<<b<< "\n";return a + b;
}
void callbackFunc2() {std::cout << __FUNCTION__ <<"\n" ;return;
}
//fun1(callbackFunc1, 10, 20);
//fun2(callbackFunc2); function
void fun1(std::function<int(int,int)> fup,int m,int n)
{int ret=(fup)(m,n);std::cout << __FUNCTION__ << " a+b " << ret << "\n";
}
void fun2(std::function<void(void)> fup)
{(fup)();
}//fun1([](int a,int b) {
//std::cout << __FUNCTION__ << " " << a << " " << b << "\n";
//return a + b;
//},10,20);
//fun2([]() {
//std::cout << __FUNCTION__ <<" no name function"<< "\n";
//});
模板函数
template <typename T>T const& Max(T const& a, T const& b){return a < b ? b : a;}template <typename T>
std::string BOOLTOSTR(T aa)
{if(aa) return "true";else return "false";
}#include <algorithm>
template <typename T>
T stdMax(std::initializer_list< T > _Ilist){return std::max(_Ilist);}#include <algorithm>
int stdMax(std::initializer_list<int> _Ilist) {return std::max(_Ilist);}
c++11之STL容器相关推荐
- C++ 笔记(19)— 标准模板库(STL容器、STL迭代器、STL算法、STL容器特点、STL字符串类)
C++ 标准库可以分为两部分: 标准函数库: 这个库是由通用的.独立的.不属于任何类的函数组成的.函数库继承自 C 语言. 面向对象类库: 这个库是类及其相关函数的集合. C++ 标准库包含了所有的 ...
- C++ STL容器——序列式容器(array、vector、deque、list)
概述 1.C++ STL的容器分为三种,序列式容器,关联式容器,无序式容器,这里先说说常用的序列式容器. 2.array,vector,deque,list,forward_list这几种都是序列式容 ...
- STL容器底层数据结构的实现
C++ STL 的实现: 1.vector 底层数据结构为数组 ,支持快速随机访问 2.list 底层数据结构为双向链表,支持快速增删 3.deque 底层 ...
- 海量数据处理算法 各种STL容器使用的数据结构剖析
教你如何迅速秒杀掉:99%的海量数据处理面试题 转载于:结构之法算法之道blog 前言 一般而言,标题含有"秒杀","99%","史上最全/最强&qu ...
- STL容器底层数据结构
STL容器底层数据结构 1.vector 底层数据结构为数组 ,支持快速随机访问 2.list 底层数据结构为双向链表,支持快速增删 3.deque 底层数据结构为一个中央控制器和多个缓冲区,详细见S ...
- c++ hashset的用法_c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
- C++常用STL容器
C++常用STL容器 vector 向量容器 二维数组指针.二维向量 pair 对 list 双向列表 map 表 unordered_map 哈希表 set 集合 unordered_set 哈希集 ...
- STL容器的emplace操作
STL容器的emplace操作 emplace是c++11引入的成员,作用是优化存储类的vector,deque,list容器 c.emplace_back(构造函数的参数) //向尾部添加一个对象, ...
- C++ STL容器 —— map/multimap 用法详解
C++ STL容器 -- map/multimap 用法详解 写在前面:近期正在学习C++的STL容器,因此在这里做一下日志记录,主要介绍一些容器基本成员函数的用法, 配上实际用例,并不涉及原理.但别 ...
最新文章
- python基础(十二):正则、re模块、贪婪和非贪婪
- PHP的display_errors与error_reporting
- de.hybris.platform.servicelayer.dto.converter.ConversionException
- 【HDU】2795 Billboard
- cnn stride and padding_彻底搞懂CNN中的卷积和反卷积
- AttributeError: Can't pickle local object 'BaseDataset.img_transformer.locals.lambda'
- JVisualVM的使用教程
- Tableau开始试用期后的破解方法
- 视频像素、分辨率、清晰度关系及区别
- 关于win11右键的慢问题的建议
- 判断文本框输入中文超过10个英文数字超过20个
- 《路由器开发 - 路由器刷机指南》优酷路由宝 YK-L1刷机
- 透过容抗来看电容量和频率的关系
- snprintf()函数探讨
- 等式约束问题-拉格朗日乘子法
- SQL 02 基础查询与排序
- CentOS配置本地yum源/阿里云yum源/163yuan源
- Other | PPTV电视安装第三方应用教程
- 四川翌加:抖音发日常和发作品哪里不一样
- 输入一个字符自动回车....