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)();
}
//demo
int 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);}