// 忽略警告
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <iostream>
#include <hash_set>
#include <hash_map>
#include <boost/assign.hpp>//map_list_of
#include <boost/typeof/typeof.hpp>//BOOST_AUTO
using namespace boost;
using namespace std;
template<typename T>
void fun()
{
//T hs(std::initializer_list<int>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
T hs = (boost::assign::list_of(0), 1, 2, 3, 4, 5, 6, 7, 8, 9);
for (T::iterator it = hs.begin(); it != hs.end(); ++it)
{
cout << *it << endl;
}
cout << "hs.size():" << hs.size() << endl;// 10
hs.insert(8);// 8已经存在
cout << "hs.size():" << hs.size() << endl;// 10
hs.insert(-1);// 插入-1
cout << "hs.size():" << hs.size() << endl;// 11
cout << *hs.find(8) << endl;//8
hs.erase(-1);
cout << "hs.size():" << hs.size() << endl;// 10
}
template<typename T>
void fun1()
{
using namespace boost::assign;
T um = map_list_of(1, "one")(2, "two")(3, "three");
BOOST_AUTO(it, um.begin());
for (; it != um.end(); ++it)
{
cout << it->first << ":" << it->second << endl;
}
um.insert(make_pair(3, "threeeee"));// key为3已存在,无效插入
cout << "um.size(): " << um.size() << endl;// 3
um.insert(make_pair(6, "six"));
cout << "um.size(): " << um.size() << endl;// 4
cout << "um[6] = " << um[6] << endl;// six
um[8] = "eight";
um[6] = "sixxxxxxxxxxx";
BOOST_AUTO(it1, um.begin());
for (; it1 != um.end(); ++it1)
{
cout << it1->first << ":" << it1->second << endl;
}
um.erase(6);
cout << "um.size(): " << um.size() << endl;// 4
}
int main()
{
cout << "----------------hash_set----------------------" << endl;
fun<hash_set<int, hash_compare<int, less<int>>, allocator<int>>>();
cout << "----------------unordered_set----------------------" << endl;
fun<unordered_set<int>>();
cout << "----------------hash_map----------------------" << endl;
fun1<hash_map<int, string>>();
cout << "----------------unordered_map----------------------" << endl;
fun1<unordered_map<int, string>>();
return 0;
}