您的位置:首页 > 大数据 > 人工智能

stl-map stl-pair

2014-02-22 18:58 316 查看

pair

模板类,用于创建 键/值 关联对。

//pair
#include
#include
#include
#include
using namespace std;

int main(int argc, char *argv[])
{
pair CppPrimer("Stanlly B.Lippman",120);
cout<
struct pair
{
_T1 first;
_T2 second;
}
#endif

map

映射。详尽参考见;http://www.cplusplus.com/reference/map/map/

注意map[key]=value这种简便形式。

#include
#include
using namespace std;
int main()
{
map m_map;
string str1="hi",str2;
if(m_map.find(str1)==m_map.end())
cout<<"not found";
m_map[str1]=3;
cout<

注意count() 与 find()函数

#include
#include
#include
using namespace std;
int main(){
map m;
m[1]='a',m[2]='b';
for(auto x:m) cout<0) {
cout<<"m contains key:2\n";
map::iterator it= m.find(2);
cout<<"the key:2 is corresponding to value:"<second;
}
return 0;
}
/*
1a
2b
-----------
m does NOT contain key:3
m contains key:2
the key:2 is corresponding to value:b
Process returned 0 (0x0)   execution time : 0.158 s
Press any key to continue.

*/


hash_map

哈希映射

hash_map的用法和map是一样的,提供了 insert,size等操作,并且里面的元素也是以pair类型来存贮的。虽然对外部提供的函数和数据类型是一致的,但是其底层实现是完全不同的,map底层的数据结构是rb_tree而hash_map却是哈希表。

#include
#include
#include
using namespace std;

int main(){
hash_mapx;
x.insert(pair(3,4));
x.insert(hash_map::value_type(1,2));
cout<
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: