map与unordered_map
来源:互联网 发布:gpu优化工程师 编辑:程序博客网 时间:2024/06/03 13:38
Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!
1. map最基本的构造函数;
map<string , int >mapstring; map<int ,string >mapint;
map<sring, char>mapstring; map< char ,string>mapchar;
map<char ,int>mapchar; map<int ,char >mapint;
2. map添加数据;
map<int ,string> maplive;
1.maplive.insert(pair<int,string>(102,"aclive"));
2.maplive.insert(map<int,string>::value_type(321,"hai"));
3, maplive[112]="April";//map中最简单最常用的插入添加!
3,map中元素的查找:
find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
map<int ,string >::iterator l_it;;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else cout<<"wo find 112"<<endl;
4,map中元素的删除:
如果删除112;
map<int ,string >::iterator l_it;;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else maplive.erase(l_it); //delete 112;
5,map中 swap的用法:
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
For example:
#include <map>
#include <iostream>
using namespace std;
int main( )
{
map <int, int> m1, m2, m3;
map <int, int>::iterator m1_Iter;
m1.insert ( pair <int, int> ( 1, 10 ) );
m1.insert ( pair <int, int> ( 2, 20 ) );
m1.insert ( pair <int, int> ( 3, 30 ) );
m2.insert ( pair <int, int> ( 10, 100 ) );
m2.insert ( pair <int, int> ( 20, 200 ) );
m3.insert ( pair <int, int> ( 30, 300 ) );
cout << "The original map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter->second;
cout << "." << endl;
// This is the member function version of swap
//m2 is said to be the argument map; m1 the target map
m1.swap( m2 );
cout << "After swapping with m2, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
cout << "After swapping with m2, map m2 is:";
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
// This is the specialized template version of swap
swap( m1, m3 );
cout << "After swapping with m3, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
}
6.map的sort问题:
Map中的元素是自动按key升序排序,所以不能对map用sort函数:
For example:
#include <map>
#include <iostream>
using namespace std;
int main( )
{
map <int, int> m1;
map <int, int>::iterator m1_Iter;
m1.insert ( pair <int, int> ( 1, 20 ) );
m1.insert ( pair <int, int> ( 4, 40 ) );
m1.insert ( pair <int, int> ( 3, 60 ) );
m1.insert ( pair <int, int> ( 2, 50 ) );
m1.insert ( pair <int, int> ( 6, 40 ) );
m1.insert ( pair <int, int> ( 7, 30 ) );
cout << "The original map m1 is:"<<endl;
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << m1_Iter->first<<" "<<m1_Iter->second<<endl;
}
The original map m1 is:
1 20
2 50
3 60
4 40
6 40
7 30
请按任意键继续. . .
7, map的基本操作函数:
C++ Maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数
#include <string>
#include <iostream>
#include <unordered_map>
using namespace std;
class humanInfo
{
public:
humanInfo();
humanInfo(string personName, u16 personage) :name(personName), age(personage) {}
humanInfo(string personName) :name(personName) {}
humanInfo(u16 personage) : age(personage) {}
~humanInfo();
string getname(){ return name; }
int getage(){ return age; }
private:
string name;
//string sex;
int age;
//s32 height;
//s32 weight;
};
typedef unordered_map<string, humanInfo *> maplive;//使用类作为内容,string为关键字。
typedef unordered_map<string, humanInfo *>::iterator mapIter;//迭代,用指针结构查看。
typedef unordered_map<string, humanInfo *>::const_iterator mapconst;//
int _tmain(int argc, _TCHAR* argv[])
{
maplive m1;
mapIter miter;
char temp;
m1.insert(pair<string, humanInfo *>("xiao5", new humanInfo("xiaoming", 10)));//初始化,pair配对的意思,考虑泄露(new)
m1.insert(pair<string, humanInfo *>("xiao1", new humanInfo("xiaohua", 6)));//这类初始化,如果存在,保持之前的内容
m1.insert(pair<string, humanInfo *>("xiao2", new humanInfo("xiaocao", 7)));
m1.insert(pair<string, humanInfo *>("xiao3", new humanInfo("xiaohuang", 1)));
m1.insert(pair<string, humanInfo *>("xiao4", new humanInfo("xiaozhang", 20)));
m1["xiao6"] = new humanInfo("xiaoli", 20);//也可以作为初始化,也可以进行对应修改。
//m1["xiao3"] = new humanInfo("xiaoli", 20);
cout << "The original map m1 is:" << m1.size() << endl;
for (miter = m1.begin(); miter != m1.end(); miter++)//轮询
cout << miter->first << " " << ((humanInfo *)(miter->second))->getname() << " " << ((humanInfo *)(miter->second))->getage() << endl;
miter = m1.find("xiao3");
if (miter != m1.end())
{
miter1 = miter;
miter++;
cout << "find:" << miter1->first << " " << miter1->second << endl;
m1.erase(miter1);//如果删除了miter1,再miter1++会死掉。
}
cout << "The original map m1 is1:" << endl;
for (miter = m1.begin(); miter != m1.end(); miter++)
cout << miter->first << " " << ((humanInfo *)(miter->second))->getname() << " " << ((humanInfo *)(miter->second))->getage() << endl;
cin >> temp;
temp = 1;
return 0;
}
The original map m1 is:6
xiao5 xiaoming 10
xiao1 xiaohua 6
xiao2 xiaocao 7
xiao3 xiaohuang 1
xiao4 xiaozhang 20
xiao6 xiaoli 20
find:xiao3 007C0900
The original map m1 is1:
xiao5 xiaoming 10
xiao1 xiaohua 6
xiao2 xiaocao 7
xiao4 xiaozhang 20
xiao6 xiaoli 20
遍历,单位us1001K10K100K1M10Mstd::map1176842116031557001771906std::ext/hash_map474304218398804703444781575std::tr1::unordered_map112121
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <list>
- #include <map>
- #include <sys/time.h>
- #include <ext/hash_map>
- #include <tr1/unordered_map>
- namespace zl
- { //{{{
- struct equal_to
- {
- bool operator()(const char* s1, const char* s2) const
- {
- return strcmp(s1, s2) == 0;
- }
- };
-
- struct hash_string
- : public std::unary_function<std::string, std::size_t>
- {
- std::size_t
- operator()(const std::string& __s) const
- #ifdef __linux__
- { return std::tr1::Fnv_hash<>::hash(__s.data(), __s.length()); }
- #else
- { return std::tr1::_Fnv_hash<>::hash(__s.data(), __s.length()); }
- #endif
- };
-
- struct hash_charptr
- : public std::unary_function<const char*, std::size_t>
- {
- std::size_t
- operator()(const char* __s) const
- #ifdef __linux__
- { return std::tr1::Fnv_hash<>::hash(__s, strlen(__s)); }
- #else
- { return std::tr1::_Fnv_hash<>::hash(__s, strlen(__s)); }
- #endif
- };
- }//}}}
- typedef std::list<std::string> string_list;
- typedef std::map<std::string, int> string_map;
- typedef __gnu_cxx::hash_map<std::string, int, zl::hash_string> string_hash_map;
- typedef std::tr1::unordered_map<std::string, int> string_unordered_map;
- void fill_list(string_list& slist, size_t count);
- uint64_t current_usec();
- int main( int argc, char* argv[] )
- {
- if (argc != 2 && argc != 3)
- {
- fprintf(stderr, "Usage:%s test_count rehash\n", argv[0]);
- fprintf(stderr, "For example:%s 10000 rehash\n", argv[0]);
- return -1;
- }
- size_t count = atoi(argv[1]);
- bool rehash = false;
- if (argc == 3)
- {
- rehash = true;
- }
- string_map smap;
- string_hash_map shash_map;
- string_unordered_map sunordered_map;
- if (rehash)
- {
- sunordered_map.rehash(count);
- }
- string_list slist;
- fill_list(slist, count);
- uint64_t start = 0;
- uint64_t end = 0;
- uint64_t map_insert_us = 0;
- uint64_t hash_map_insert_us = 0;
- uint64_t unordered_map_insert_us = 0;
- uint64_t map_traverse_us = 0;
- uint64_t hash_map_traverse_us = 0;
- uint64_t unordered_map_traverse_us = 0;
- uint64_t map_find_us = 0;
- uint64_t hash_map_find_us = 0;
- uint64_t unordered_map_find_us = 0;
- uint64_t map_delete_us = 0;
- uint64_t hash_map_delete_us = 0;
- uint64_t unordered_map_delete_us = 0;
- // Insert test
- {//{{{
- string_list::iterator it(slist.begin());
- string_list::iterator ite(slist.end());
- //map insert
- start = current_usec();
- for (int i = 0; it != ite; ++it, ++i)
- {
- smap[*it] = i;
- }
- end = current_usec();
- map_insert_us = end - start;
- //hash_map insert
- it = slist.begin();
- start = current_usec();
- for (int i = 0; it != ite; ++it, ++i)
- {
- shash_map[*it] = i;
- }
- end = current_usec();
- hash_map_insert_us = end - start;
- //unordered_map insert
- it = slist.begin();
- start = current_usec();
- for (int i = 0; it != ite; ++it, ++i)
- {
- shash_map[*it] = i;
- }
- end = current_usec();
- unordered_map_insert_us = end - start;
- }//}}}
- // Traverse test
- {//{{{
- //map traverse
- {
- string_map::iterator it(smap.begin());
- string_map::iterator ite(smap.end());
- start = current_usec();
- for (int i = 0; it != ite; ++it)
- {
- i++;
- }
- end = current_usec();
- map_traverse_us = end - start;
- }
- //hash_map traverse
- {
- string_hash_map::iterator it(shash_map.begin());
- string_hash_map::iterator ite(shash_map.end());
- start = current_usec();
- for (int i = 0; it != ite; ++it)
- {
- i++;
- }
- end = current_usec();
- hash_map_traverse_us = end - start;
- }
- //unordered_map traverse
- {
- string_unordered_map::iterator it(sunordered_map.begin());
- string_unordered_map::iterator ite(sunordered_map.end());
- start = current_usec();
- for (int i = 0; it != ite; ++it)
- {
- i++;
- }
- end = current_usec();
- unordered_map_traverse_us = end - start;
- }
- }//}}}
- // Find test
- {//{{{
- string_list::iterator it(slist.begin());
- string_list::iterator ite(slist.end());
- //map find
- start = current_usec();
- for (int i = 0; it != ite; ++it, ++i)
- {
- smap[*it] = i;
- }
- end = current_usec();
- map_find_us = end - start;
- //hash_map find
- it = slist.begin();
- start = current_usec();
- for (int i = 0; it != ite; ++it, ++i)
- {
- shash_map[*it] = i;
- }
- end = current_usec();
- hash_map_find_us = end - start;
- //unordered_map find
- it = slist.begin();
- start = current_usec();
- for (int i = 0; it != ite; ++it, ++i)
- {
- shash_map[*it] = i;
- }
- end = current_usec();
- unordered_map_find_us = end - start;
- }//}}}
- // Delete test
- {//{{{
- string_list::iterator it(slist.begin());
- string_list::iterator ite(slist.end());
- //map delete
- start = current_usec();
- for (int i = 0; it != ite; ++it, ++i)
- {
- smap.erase(*it);
- }
- end = current_usec();
- map_delete_us = end - start;
- //hash_map delete
- it = slist.begin();
- start = current_usec();
- for (int i = 0; it != ite; ++it, ++i)
- {
- shash_map.erase(*it);
- }
- end = current_usec();
- hash_map_delete_us = end - start;
- //unordered_map delete
- it = slist.begin();
- start = current_usec();
- for (int i = 0; it != ite; ++it, ++i)
- {
- shash_map.erase(*it);
- }
- end = current_usec();
- unordered_map_delete_us = end - start;
- }//}}}
- //stat output
- std::cout << " insert, count " << count << std::endl;
- std::cout << " std::map " << map_insert_us << " us\n";
- std::cout << " std::ext/hash_map " << hash_map_insert_us << " us\n";
- std::cout << "std::tr1::unordered_map " << unordered_map_insert_us << " us\n";
- std::cout << "\n";
- std::cout << " traverse, count " << count << std::endl;
- std::cout << " std::map " << map_traverse_us << " us\n";
- std::cout << " std::ext/hash_map " << hash_map_traverse_us << " us\n";
- std::cout << "std::tr1::unordered_map " << unordered_map_traverse_us << " us\n";
- std::cout << "\n";
- std::cout << " find, count " << count << std::endl;
- std::cout << " std::map " << map_find_us << " us\n";
- std::cout << " std::ext/hash_map " << hash_map_find_us << " us\n";
- std::cout << "std::tr1::unordered_map " << unordered_map_find_us << " us\n";
- std::cout << "\n";
- std::cout << " delete, count " << count << std::endl;
- std::cout << " std::map " << map_delete_us << " us\n";
- std::cout << " std::ext/hash_map " << hash_map_delete_us << " us\n";
- std::cout << "std::tr1::unordered_map " << unordered_map_delete_us << " us\n";
-
- return 0;
- }
- void fill_list(string_list& slist, size_t count)
- {
- for (size_t i = 0; i < count; ++i)
- {
- std::ostringstream oss;
- oss << i;
- //slist.push_back(MD5::getHexMD5(oss.str().c_str(), oss.str().length()));
- slist.push_back(oss.str());
- }
- }
- uint64_t current_usec()
- {
- struct timeval tv;
- gettimeofday( &tv, NULL );
- return tv.tv_sec * 1000 * 1000 + tv.tv_usec;
- }
- map与unordered_map
- map与unordered_map
- map与unordered_map
- map与unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- STL map与Boost unordered_map
- boost::unordered_map与stl::map
- STL map与Boost unordered_map
- Cilk所有内容、资料
- 【POJ1379】Run Away 模拟退火
- 央行降准 A股牛市再加码?
- unity 学习 做的例子截图
- hdoj 1874 畅通工程续 【最短路径】
- map与unordered_map
- Windows 7系统安装MySQL5.5.21图解
- select、poll、epoll之间的区别总结[整理]
- Device Tree(一):背景介绍
- js菜鸟正则表达式和function类型
- Servlet3.0新特性---异步支持
- 算法复杂度
- WiFi流量劫持—— 浏览任意页面即可中毒!
- Linux设备模型(8)_platform设备