1. STL的六大组件:

  容器(Container)

  算法(Algorithm)

  迭带器(Iterator)

  仿函数(Function object)

  适配器(Adaptor)

  空间配置器(allocator)

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std; int main()
{
//容器:
vector<int> myint{ ,,,, };
myint.push_back(); //算法:
for_each(myint.begin(), myint.end(), [](int x) {cout << x <<" "; });
cout << endl; //迭带器:
auto it = myint.begin() + ;
for (auto ib = myint.begin(), ie = myint.end(); ib != ie; ib++)
{
cout << *ib << " ";
}
cout << endl; list<int> mylist{ ,,,, };
mylist.push_front();
//auto ib1 = mylist.begin() + 2; //链表每次只能前进一个
auto ib1 = mylist.begin()++; //迭带器自动适应容器 for_each(mylist.begin(), mylist.end(), [](int x) {cout << x << " "; });
cout << endl; system("pause");
return ;
}

    

2. 容器类别:

  序列式容器:排列次序取决于插入时机和位置

  关联式容器:排列次序取决于特定准则

    

  2.1 线性容器 ( array ):

#include <iostream>
#include <array>
#include <algorithm>
using namespace std; //array容器在栈区,自动回收内存,不能增删查改,
int main()
{
array<int, > MyArr{ ,,,,,,,,, };
for (auto ib = MyArr.begin(), ie = MyArr.end(); ib != ie; ib++)
{
//cout << typeid(ib).name() << ": ";
cout << *ib << " ";
}
cout << endl; for (auto i:MyArr)
{
cout << i << " ";
}
cout << endl; for_each(MyArr.begin(), MyArr.end(), [](int & x) {x += ; });
for_each(MyArr.begin(), MyArr.end(), [](int x) {cout << x << " "; });
cout << endl; system("pause");
return ;
}

    

  2.2 线性容器 ( vector ):

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; //vector在堆区,可以增删查改,
int main()
{
vector<int> MyVec{ ,,,,,,,,, };
for (auto ib = MyVec.begin() + , ie = MyVec.end(); ib != ie; ib++) //vector迭带器可以+3
{
cout << *ib << " ";
}
cout << endl; for (auto i : MyVec)
{
cout << i << " ";
}
cout << endl; for_each(MyVec.begin(), MyVec.end(), [](int & x) {x += ; });
for_each(MyVec.begin(), MyVec.end(), [](int x) {cout << x << " "; });
cout << endl; system("pause");
return ;
}

    

  2.3 线性容器 ( forwardlist ):单链表( forward:前进 )==>只能前进,不能后退

#include <iostream>
#include <forward_list> //单链表(forward:前进)==>只能前进,不能后退
#include <algorithm>
using namespace std; int main()
{
forward_list<int> my{ ,,,,,,,,, };
for (auto ib = my.begin(), ie = my.end(); ib != ie; ib++) //ib++可以;但是ib--不行
{
cout << *ib << " ";
}
cout << endl; for (auto i : my)
{
cout << i << " ";
}
cout << endl; for_each(my.begin(), my.end(), [](int & x) {x += ; });
for_each(my.begin(), my.end(), [](int x) {cout << x << " "; });
cout << endl; system("pause");
return ;
}

    

  2.4 线性容器 ( list ):双链表==>既可以前进,也可以后退

#include <iostream>
#include <list> //双链表
#include <algorithm>
using namespace std; int main()
{
list<int> my{ ,,,,,,,,, };
for (auto ib = my.begin(), ie = my.end(); ib != ie; ib++) //ib++,ib--可以;但是ib+3不行
{
cout << *ib << " ";
}
cout << endl; for (auto i : my)
{
cout << i << " ";
}
cout << endl; for_each(my.begin(), my.end(), [](int & x) {x += ; });
for_each(my.begin(), my.end(), [](int x) {cout << x << " "; });
cout << endl; system("pause");
return ;
}

    

  2.5 线性容器 (deque): 双端队列(分块) ==>性能综合的要求(增、删、查、改)

#include <iostream>
#include <deque> //双端队列(分块数据模型),在堆上
#include <algorithm>
using namespace std; int main()
{
deque<int> my{ ,,,,,,, };
cout << "最大容量:" << my.max_size() << endl; //最大容量由内存决定
cout << "当前容量:" << my.size() << endl;
cout << "第一个元素:" << my.front() << endl;
cout << "最后一个元素:" << my.back() << endl; for (auto ib = my.begin(), ie = my.end(); ib != ie; ib++)
{
cout << *ib << " ";
}
cout << endl; for (auto i : my)
{
cout << i << " ";
}
cout << endl; for_each(my.begin(), my.end(), [](int & x) {x += ; });
for_each(my.begin(), my.end(), [](int x) {cout << x << " "; });
cout << endl; system("pause");
return ;
}

    

    deque的应用:

#include <iostream>
#include <deque> //双端队列(分块数据模型),在堆上
#include <algorithm>
using namespace std; int main()
{
deque<int> my{ ,,,,,,, };
for_each(my.begin(), my.end(), [](int x) {cout << x << " "; });
cout << endl; my.push_back(); //尾部插入数据10
for_each(my.begin(), my.end(), [](int x) {cout << x << " "; });
cout << endl; my.push_front(); //头部插入
for_each(my.begin(), my.end(), [](int x) {cout << x << " "; });
cout << endl; my.insert(my.begin() + , ); //任意位置插入
for_each(my.begin(), my.end(), [](int x) {cout << x << " "; });
cout << endl; my.pop_front(); //头部删除
my.pop_back(); //尾部删除
for_each(my.begin(), my.end(), [](int x) {cout << x << " "; });
cout << endl; my.erase(my.begin() + ); //任意位置删除
for_each(my.begin(), my.end(), [](int x) {cout << x << " "; });
cout << endl; for (int i = ; i < my.size(); i++)
{
cout << my[i] << " "; //可以用[]访问其中的元素,类模板中重载了[]
}
cout << endl; for (auto cb = my.cbegin(), ce = my.cend(); cb != ce; cb++) //cbegin(),cend()是常量迭代器;c表示const(只读)
{
cout << *cb << " ";
//*cb = 1; //常量迭代器,只能读,不能写
}
cout << endl; my.clear(); //清空
for_each(my.begin(), my.end(), [](int x) {cout << x << " "; });
cout << endl; system("pause");
return ;
}

    

  2.6 线性容器 ( stack ): 栈

#include <iostream>
#include <stack> //栈
#include <list>
#include <vector>
#include <algorithm>
using namespace std; int main()
{
stack<int> myStack; //数组栈;stack默认底层是有deque实现的(因为deque的删除、插入性能也比较高)
stack<int, list<int>> myListStack; //链式栈
stack<int, vector<int>> myVecStack; //可以任意指定底层的容器 myStack.push();
myStack.push();
myStack.push();
myStack.push();
myStack.push(); while (!myStack.empty())
{
cout << myStack.top() << endl; //打印栈顶元素
myStack.pop(); } system("pause");
return ;
}

    

  2.7 线性容器 ( queue): 队列

#include <iostream>
#include <queue> //队列
#include <list>
#include <vector>
#include <algorithm>
using namespace std; int main()
{
queue<int> myQ; //queue默认底层是deque实现的
queue<int, list<int>> myListQ; //指定底层容器为list
queue<int, vector<int>> myVecQ; myQ.push();
myQ.push();
myQ.push();
myQ.push();
/*
for (auto i : myQ) //队列不允许迭代器访问
{
cout << i << " ";
}
cout << endl;
*/ while (!myQ.empty())
{
cout << myQ.front() << endl; //打印栈顶元素
myQ.pop();
} system("pause");
return ;
}

    

  2.8 线性容器 ( string ): 字符串

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std; int main()
{
string str1 = "";
string str2 = "";
string str3 = str1 + str2; str3.push_back('X');
cout << str3 << endl; const char *p = str3.c_str(); //只能用只读指针const char *,返回内部指针
cout << p << endl;
cout << *p << endl; for (auto i:str3)
{
cout << i << " ";
}
cout << endl; for (auto cb = str3.cbegin(), ce = str3.cend(); cb != ce; cb++)
{
cout << *cb << " ";
}
cout << endl; system("pause");
return ;
}

    

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std; int main()
{
string s1("12345hello hello hahaha");
s1.insert(s1.begin() + , 'X'); //任意位置插入字符
cout << s1 << endl; s1.erase(s1.begin() + , s1.begin() + ); //删除从s1.begin() + 2到s1.begin() + 4的字符
cout << s1 << endl; s1.erase(, ); //删除从1开始的2个字符
cout << s1 << endl; s1.replace(, , "world"); //从0位置开始,替换2个位置,替换内容为world
cout << s1 << endl; s1.replace(s1.begin(), s1.begin()+, "China");
cout << s1 << endl; s1.replace(, , " haha "); //使用replace插入字符串
cout << s1 << endl; cout << s1.find("ha") << endl; //查找
cout << s1.find("ha",) << endl; //指定位置开始查找 cout << s1.rfind("ha") << endl; //反向查找 cout << s1.find_first_of("1234a") << endl; //查找第一个属于"1234a"的字符
cout << s1.find_first_not_of("1234a") << endl; //第一个不属于的字符 cout << s1.length() << endl; //字符串的长度
cout << s1.size() << endl; //总共分配的长度 s1.empty(); //清空
cout << s1 << endl;
cout << s1.length() << endl; system("pause");
return ;
}

    

3. 非线性容器:

  3.1 优先队列( priority_queue ):

#include <iostream>
#include <queue>
#include <list>
#include <vector>
#include <algorithm>
using namespace std; int main()
{
priority_queue<int> myQ; //优先队列 myQ.push();
myQ.push();
myQ.push();
myQ.push();
while (!myQ.empty())
{
cout << myQ.top() << endl; //打印栈顶元素
myQ.pop();
} system("pause");
return ;
}

    

#include <iostream>
#include <queue>
#include <list>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std; struct getMoney
{
char *com; //公司
int money; //工资
}; struct lessX
{
bool operator ()(struct getMoney &m1, struct getMoney &m2) //伪函数
{
//return m1.money < m2.money; //按money排序
if (strcmp(m1.com, m2.com) >= ) //按com排序
return true;
else
return false;
}
}; int main()
{
priority_queue<getMoney,deque<getMoney>, lessX> myQ; //优先队列
getMoney getm[] = { {"google",},{"baidu",},{"",},{"tencent",} }; for (auto i:getm)
{
myQ.push(i);
}
while (!myQ.empty())
{
cout << myQ.top().com << " " << myQ.top().money << endl;
myQ.pop();
} system("pause");
return ;
}

    

  3.2 位处理容器( bitset ):

#include <iostream>
#include <bitset> //处理位运算
#include <string>
using namespace std; int main()
{
bitset<> mybit(); //1100 1101 8位 for (int i = ; i >= ; i--) //C++中 高字节在低位 ==>需要逆序打印
{
cout << mybit[i];
if (i % == ) //每4位隔开,便于观察
{
cout << " ";
}
}
cout << endl; system("pause");
return ;
}

    

#include <iostream>
#include <bitset> //处理位运算
#include <string>
using namespace std; int main()
{
char ch = -;
bitset<> mybit(ch); //8位
//1000 0001 原码
//1111 1110 反码
//1111 1111 补码 for (int i = ; i >= ; i--) //C++中 高字节在低位 ==>需要逆序打印
{
cout << mybit[i];
if (i % == ) //每4位隔开,便于观察
{
cout << " ";
}
}
cout << endl; system("pause");
return ;
}

    

#include <iostream>
#include <bitset> //处理位运算
#include <string>
using namespace std; int main()
{
float fl = 19.875;
cout << (void *)&fl << endl; //浮点数无法处理,只能处理整数,浮点数会转化为整数
bitset<> mybit(fl); //32位 float占4字节 for (int i = ; i >= ; i--) //C++中 高字节在低位 ==>需要逆序打印
{
cout << mybit[i];
if (i % == ) //每4位隔开,便于观察
{
cout << " ";
}
}
cout << endl; system("pause");
return ;
}

    

   3.3 集合( set ):==>不允许重复的数据

#include <iostream>
#include <set> //底层是红黑树,自动保证平衡,自动生成平衡查找二叉树
#include <cstdlib>
using namespace std; int main()
{
set<int> myset{ ,,,, }; //set是用红黑树(会自动调整为平衡二叉查找树)实现的
myset.insert();
myset.insert();
myset.insert(); //重复数据会被忽略 myset.erase(); //直接删除 for (auto i:myset)
{
cout << i << " ";
}
cout << endl; for (auto ib = myset.begin(), ie = myset.end(); ib != ie; ib++)
{
cout << *ib << " ";
}
cout << endl; for (auto rb = myset.rbegin(), re = myset.rend(); rb != re; rb++) //反向迭代器
{
cout << *rb << " ";
}
cout << endl; auto it = myset.find(); //查找
if (it != myset.end())
{
cout << "find " << *it << endl;
}
else
{
cout << "not find" << endl;
} cout << myset.size() << endl; //元素个数
myset.empty() ; //清空 auto it1 = myset.find(); //查找
if (it1 != myset.end())
{
myset.erase(it1); //根据迭代器获取位置,删除
}
for (auto i : myset)
{
cout << i << " ";
}
cout << endl; system("pause");
return ;
}

    

  实现复杂数据类型自动排序:

#include <iostream>
#include <set> //底层是红黑树,自动保证平衡,自动生成平衡查找二叉树
#include <cstdlib>
#include <cstring>
using namespace std; struct strless
{
bool operator()(const char *str1, const char *str2) //伪函数
{
return (strcmp(str1, str2) < );
}
}; int main()
{
const char *p[] = { "calc","notepad","run","go","" };
set<const char *, strless> myset(p, p + , strless()); //实现字符串的自动排序
myset.insert("");
for (auto i:myset)
{
cout << i << " ";
}
cout << endl; system("pause");
return ;
}

    

  3.4 多集合( multiset ):==>允许有重复数据

#include <iostream>
#include <set>
#include <cstdlib>
#include <cstring>
using namespace std; struct strless
{
bool operator()(const char *str1, const char *str2) //伪函数
{
return (strcmp(str1, str2) < );
}
}; int main()
{
multiset<int> myset{ ,,,,,,, }; //底层是红黑树,每个元素是一个链表
myset.insert();
myset.insert();
myset.insert(); for (auto i:myset)
{
cout << i << " ";
}
cout << endl; auto it = myset.equal_range(); //返回值是pair类型 pair<链表起点,链表终点>
//cout << typeid(it).name() << endl;
for (auto i = it.first; i != it.second; i++)
{
cout << *i << " ";
}
cout << endl; auto it1 = myset.find(); //只查找链表的第一个元素
cout << "find: "<<*it1 << endl; /*实现复杂数据类型*/
const char *p[] = { "calc","notepad","run","go","go","go","" };
multiset<const char *, strless> myset1(p, p + , strless()); //实现字符串的自动排序
myset1.insert("");
for (auto i : myset1)
{
cout << i << " ";
}
cout << endl; system("pause");
return ;
}

    

  3.5 哈希表( hash_set ):已被unordered_set 替代

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS    //现在用新的容器unordered_set取代了hash_set,需要定义这个宏
#include <iostream>
#include <hash_set> //底层是哈希表,是无序的
#include <cstring>
using namespace std; struct strless
{
bool operator()(const char *str1, const char *str2) const //伪函数
{
return (strcmp(str1, str2) == ); //哈希表只能处理相等
}
}; int main()
{
//哈希表,不需要考虑碰撞,查询的效率最高
hash_set<int> myset{ ,,,,,,, };
myset.insert();
for (auto i:myset)
{
cout << i << " ";
}
cout << endl; for (auto ib = myset.begin(), ie = myset.end(); ib != ie; ib++)
{
cout << *ib << " ";
}
cout << endl; for (auto rb = myset.rbegin(), re = myset.rend(); rb != re; rb++) //反向迭代器
{
cout << *rb << " ";
}
cout << endl; cout << myset.size() << endl; //元素个数
cout << myset.bucket_count() << endl; //64是哈希表的大小 /*实现复杂数据类型*/
hash_set<const char *, hash_compare<const char *, strless>> hs; //hash_compare<const char *, strless>设定哈希函数,根据实际需求(string)设定哈希函数
hs.insert("apple");
hs.insert("google");
hs.insert("Microsoft");
hs.insert("oracle"); auto i = hs.find("google");
if (i != hs.end())
{
cout << "find: " << *i << endl; //为什么输出apple呢???
} system("pause");
return ;
}

    

  3.6 哈希表( unordered_set ):==>不允许重复数据

#include <iostream>
#include <unordered_set> //凡是带unordered的容器底层都是哈希表实现,而不是红黑树
#include <string>
using namespace std; int main()
{
unordered_set<string> myset{ "hello","world","move","jump","abc","last","one","sun","best" }; //unordered_set是无序的,按照哈希函数进行存放 myset.erase(myset.begin()); //删除哈希表的第一个
myset.erase("hello"); //精准删除
//myset.erase(myset.begin(), myset.end()); //删除某一段 for (auto i:myset)
{
cout << i << " ";
}
cout << endl; myset.insert("love"); //插入 for (auto ib = myset.begin(), ie = myset.end(); ib != ie; ib++)
{
cout << *ib << " ";
}
cout << endl; cout << "bucket_count:" << myset.bucket_count() << endl; //哈希表长度,自动增长,没有规律
cout << "max_size:" << myset.max_size() << endl; //最大大小
cout << "size:" << myset.size() << endl; //当前大小 for (int i = ; i < myset.bucket_count(); i++) //打印整个哈希表(按bucket打印),可见unordered_set不允许重复,
{ //而unordered_multiset允许重复
cout << "bucket:" << i << ": ";
for (auto it = myset.begin(i); it != myset.end(i); it++)
{
cout << " " << *it;
}
cout << endl;
} system("pause");
return ;
}

    

    

  3.7 哈希表( unordered_multiset ):==>允许重复数据

#include <iostream>
#include <unordered_set>
#include <string>
using namespace std; int main()
{
unordered_multiset<string> myset{ "pig","pig","pig","hen" };
myset.insert("love"); //插入 for (auto i:myset)
{
cout << i << endl;
}
cout << "\n"; auto it = myset.equal_range("pig");
while (it.first != it.second) //相同元素在链表上,起点为first,终点为second
{
cout << *it.first++ << endl;
} system("pause");
return ;
}

    

  3.8 映射( map ) ==> 不允许重复 <==> 红黑树实现

#include <iostream>
#include <map> //底层是红黑树
#include <cstring>
using namespace std; //仿函数:STL六大组件之一
struct strless
{
bool operator()(const char *str1, const char *str2)
{
return (strcmp(str1, str2) < ); //字符串对比
}
}; int main()
{
map<char *, int> mymap;
mymap.insert(pair<char *, int>("司令1", ));
mymap.insert(pair<char *, int>("司令2", ));
mymap.insert(pair<char *, int>("司令3", ));
mymap.insert(pair<char *, int>("司令4", ));
mymap.insert(pair<char *, int>("司令4", )); //第一个字段,不允许有重复 for (auto i : mymap)
{
cout << i.first << " " << i.second << endl;
} for (auto cb = mymap.cbegin(), ce = mymap.cend(); cb != ce; cb++)
{
cout << (*cb).first << " " << (*cb).second << endl;
} auto it = mymap.find("司令2");
cout << "司令2: " << (*it).second << endl; /*map的排序实现*/
map<char *, int, strless> myMap; //根据strless仿函数来排序
myMap.insert(pair<char *, int>("a司令1", ));
myMap.insert(pair<char *, int>("c司令2", ));
myMap.insert(pair<char *, int>("d司令3", ));
myMap.insert(pair<char *, int>("b司令4", )); myMap.erase("b司令4"); //删除 for (auto i : myMap)
{
cout << i.first << " " << i.second << endl;
} auto ite = myMap.find("c司令2");
myMap.erase(ite,myMap.end()); //删除一段(从ite开始到end())
for (auto i : myMap)
{
cout << i.first << " " << i.second << endl;
} auto it2 = myMap.begin()++; //红黑树,链式存储
//auto it2 = myMap.begin() + 3; //只能++,不能+3 cout << myMap["a司令1"] << endl; //可以通过下标[]来访问 //myMap.clear(); //清空 system("pause");
return ;
}

    

  3.9 多映射( multimap ):==> 允许重复 <==> 红黑树实现

#include <iostream>
#include <map>
#include <cstring>
using namespace std; struct strless
{
bool operator()(const char *str1, const char *str2)
{
return (strcmp(str1, str2) < ); //字符串对比
}
}; int main()
{
multimap<char *, int, strless> mymap; //multimap允许重复 加上strless仿函数后可以进行排序
mymap.insert(pair<char *, int>("司令1", ));
mymap.insert(pair<char *, int>("司令4", ));
mymap.insert(pair<char *, int>("司令4", ));
mymap.insert(pair<char *, int>("司令2", ));
mymap.insert(pair<char *, int>("司令3", ));
mymap.insert(pair<char *, int>("司令4", ));
mymap.insert(pair<char *, int>("司令4", )); for (auto i : mymap)
{
cout << i.first << " " << i.second << endl;
}
cout << endl; auto it = mymap.equal_range("司令4");
for (auto ib = it.first, ie = it.second; ib != ie; ib++)
{
cout << (*ib).first << " " << (*ib).second << endl;
}
cout << endl; system("pause");
return ;
}

    

  3.10 哈希映射( hash_map ):是unordered_map的前身

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include <iostream>
#include <hash_map>
#include <string>
using namespace std; //增删查改与map一样,本质是哈希表
int main()
{
hash_map<string, double> mymap{ { "good",120.32 },{ "better",150.89 },{ "best",218.56 },{ "hello",75.36 },{ "kitty",187.85 } };
mymap.insert(pair<string, double>("love", 356.71)); //插入
mymap.insert(hash_map<string, double>::value_type("like", 289.48)); //插入 for (auto i : mymap)
{
cout << i.first << " " << i.second << endl;
}
cout << endl; auto it = mymap.find("hello"); //查找
if (it != mymap.end())
{
cout << it->second << endl;
} /*显示哈希表*/
for (int i = ; i < mymap.bucket_count(); i++) //打印整个哈希表(按bucket打印),可见unordered_set不允许重复,
{ //而unordered_multiset允许重复
cout << "bucket:" << i << ": ";
for (auto it = mymap.begin(i); it != mymap.end(i); it++)
{
cout << " " << it->first << " " << it->second;
}
cout << endl;
} system("pause");
return ;
}

    

  3.11 映射( unordered_map ):==> 不允许重复 <==> 哈希表实现

    若要求综合性能,选:map

    若要求最高查询性能,选:unordered_map

#include <iostream>
#include <unordered_map>
#include <string>
using namespace std; //增删查改与map一样,本质是哈希表
int main()
{
unordered_map<string, double> mymap{ {"good",120.32},{"better",150.89},{"best",218.56},{"hello",75.36},{"kitty",187.85} };
mymap.insert(pair<string, double>("love", 356.71)); //插入
mymap.insert(unordered_map<string, double>::value_type("like",289.48)); //插入 for (auto i : mymap)
{
cout << i.first << " " << i.second << endl;
}
cout << endl; auto it = mymap.find("hello"); //查找
if (it != mymap.end())
{
cout << it->second << endl;
} /*显示哈希表*/
for (int i = ; i < mymap.bucket_count(); i++) //打印整个哈希表(按bucket打印),可见unordered_set不允许重复,
{ //而unordered_multiset允许重复
cout << "bucket:" << i << ": ";
for (auto it = mymap.begin(i); it != mymap.end(i); it++)
{
cout << " " << it->first<<" "<<it->second;
}
cout << endl;
} system("pause");
return ;
}

    

  3.12 映射( unordered_multimap ):==> 允许重复 <==> 哈希表实现

#include <iostream>
#include <unordered_map>
#include <string>
#include <algorithm>
using namespace std; //增删查改与map一样,本质是哈希表,允许重复
int main()
{
unordered_multimap<string, double> mymap{ { "good",120.32 },{ "better",150.89 },{ "best",218.56 } };
mymap.insert(pair<string, double>("love", 356.71)); //插入
mymap.insert(pair<string, double>("love", 218.34));
mymap.insert(pair<string, double>("love", 568.45));
mymap.insert(unordered_map<string, double>::value_type("like", 289.48)); //插入 for (auto i : mymap)
{
cout << i.first << " " << i.second << endl;
}
cout << endl; auto it = mymap.find("hello"); //查找
if (it != mymap.end())
{
cout << it->second << endl;
} /*显示哈希表*/
for (int i = ; i < mymap.bucket_count(); i++) //打印整个哈希表(按bucket打印),可见unordered_set不允许重复,
{ //而unordered_multiset允许重复
cout << "bucket:" << i << ": ";
for (auto it = mymap.begin(i); it != mymap.end(i); it++)
{
cout << " " << it->first << " " << it->second;
}
cout << endl;
}
cout << endl; auto itx = mymap.equal_range("love");
for_each(itx.first, itx.second, [](unordered_multimap<string, double>::value_type &x) {cout << x.first << " " << x.second << endl; }); system("pause");
return ;
}

    

  3.13 用于计算的 valarray:

#include <iostream>
#include <valarray> //用于计算,计算的性能高于vector,array
#include <iomanip>
using namespace std; int main()
{
const double PI = 3.1415926535; valarray<double> val();
for (int i = ; i < ; i++)
{
val[i] = PI*i*i;
cout << val[i] << endl;
} valarray<double> valx();
for (int i = ; i < ; i++)
{
valx[i] = cos(i*PI / ); //可以直接进行科学计算cos()
cout << valx[i] << endl;
} valarray<char *> my; //拥有数组的所有功能,长处是计算 system("pause");
return ;
}

    

 4. 容器小结:

5. STL编程五的更多相关文章

  1. C++ STL编程轻松入门基础

    C++ STL编程轻松入门基础 1 初识STL:解答一些疑问 1.1 一个最关心的问题:什么是STL 1.2 追根溯源:STL的历史 1.3 千丝万缕的联系 1.4 STL的不同实现版本 2 牛刀小试 ...

  2. Linux网络编程(五)

    /*Linux网络编程(五)——多路IO复用之select() 网络编程中,使用IO复用的典型场合: 1.当客户处理多个描述字时(交互式输入以及网络接口),必须使用IO复用. 2.一个客户同时处理多个 ...

  3. socket编程五种模型

    客户端:创建套接字,连接服务器,然后不停的发送和接收数据. 比较容易想到的一种服务器模型就是采用一个主线程,负责监听客户端的连接请求,当接收到某个客户端的连接请求后,创建一个专门用于和该客户端通信的套 ...

  4. C++面向对象高级编程(五)类与类之间的关系

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. 本节主要介绍一下类与类之间的关系,也就是面向对象编程先介绍两个术语 Object Oriented Programming   OOP面向对象编 ...

  5. linux高性能服务器编程 (五) --Linux网络编程基础api

    第五章 Linux网络编程基础api 1.主机字节序和网络字节序 字节序是指整数在内存中保存的顺序.字节序分为大端字节序.小端字节序. 大端字节序:一个整数的高位字节数据存放在内存的低地址处.低位字节 ...

  6. 【C++ STL编程】queue小例子

    STL是标准化组件,现在已经是C++的一部分,因此不用额外安装什么. #include <queue> #include <iostream> using namespace ...

  7. C++ STL编程轻松入门【转载】

    1 初识STL:解答一些疑问 1.1 一个最关心的问题:什么是STL "什么是STL?",假如你对STL还知之甚少,那么我想,你一定很想知道这个问题的答案,坦率地讲,要指望用短短数 ...

  8. Rob Pike 编程五原则

    Rob Pike's 5 Rules of Programming Rule 1: You can't tell where a program is going to spend its time. ...

  9. 【Java并发编程五】信号量

    一.概述 技术信号量用来控制能够同时访问某特定资源的活动的数量,或者同时执行某一给定操作的数据.计数信号量可以用来实现资源池或者给一个容器限定边界. 信号量维护了一个许可集,许可的初始量通过构造函数传 ...

随机推荐

  1. placement new

    placement new就是把原本new做的两步工作分开来.第一步你自己分配内存,第二步你调用类的构造函数在自己分配的内存上构建新的对象. class Foo { float f; public: ...

  2. 理解UV贴图

    一.理解UV贴图UVs是驻留在多边形网格顶点上的两维纹理坐标点,它们定义了一个两维纹理坐标系统,称为UV纹理空间,这个空间用U和V两个字母定义坐标轴.用于确定如何将一个纹理图像放置在三维的模型表面.本 ...

  3. CentOS7.2部署KVM虚拟机

    转自:http://www.linuxidc.com/Linux/2017-01/140007.htm 学习了关于PostGis.OSM数据以及Mapnik相关内容,接下来将利用假期重点学习Postg ...

  4. 我所理解的 PHP Trait

    Trait 是从 PHP 5.4 加入的一种细粒度代码复用的语法.以下是官方手册对 Trait 的描述: Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承 ...

  5. docker daemon文件/etc/docker/daemon.json配置

    On Linux The default location of the configuration file on Linux is /etc/docker/daemon.json. The --c ...

  6. iOS7中的多任务I

    [改变了后台任务的运行方式] 在iOS6和之前的系统中,系统在用户退出应用后,如果应用正在执行后台任务的话,系统会保持活跃状态直到后台任务完成或者是超时以后,才会进入真正的低功耗休眠状态. 而在iOS ...

  7. mongo学习- 副本集 大多数原则

    副本集中有一个重要的概念“大多数”,意思是说,选择主节点需要大多数决定(本人亲自做了实验) 步骤: 1.开启副本集(如果没有配置好 副本集的 亲参考我的上篇文章  https://www.cnblog ...

  8. RocketMQ 服务器3模式

    22 a b-s---------------------------sh mqbroker -c $ROCKETMQ_HOME/conf/2m-2s-async/broker-a.propertie ...

  9. RocketMQ 加载配置文件

    BrokerStartup.java // 指定配置文件 if (commandLine.hasOption('c')) { String file = commandLine.getOptionVa ...

  10. SQL SERVER 2012数据库:开启防火墙导致外部无法连接数据库解决办法

    SQL SERVER 2012数据库:开启防火墙导致外部无法连接数据库解决办法 将以下代码存为OpenSqlServerPort.bat文件: netsh advfirewall firewall a ...