unsorted_map,unsorted_set如何操作

wy672858204 2014-11-12 05:25:57
已知:map数据操作如下:
1. 主键定义
class mykey
{
public:
mykey(const int& _a)
{
a = _a;
}
bool operator < (const mykey& _m) const
{
if (a < _m.a)
return true;
return false;
}
public:
int a;
}
2.变量定义
map<mykey,int> m_mymap;
3.数据插入
m_mymap.insert(map<mykey,int>::value_type(mykey(10),10));

问题:unsorted_map如何操作
同样问题:unsorted_set如何操作
在下先谢谢各位大师了!!!
...全文
348 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianghandaxue 2014-11-13
  • 打赏
  • 举报
回复
没用过,帮顶。。。。。
wy672858204 2014-11-13
  • 打赏
  • 举报
回复
谢谢您的回答,map的使用我已知道, 现在我需要的是vs2010版本新增的unsorted_map,unsorted_set的操作方法, 还望各位大师不吝赐教,贴出unsorted_map,unsorted_set自定义主键的使用方法!!
shen_wei 2014-11-13
  • 打赏
  • 举报
回复
// map_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( ) {
   using namespace std;
   map <int, int>::iterator m1_pIter, m2_pIter;

   map <int, int> m1, m2;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );
   m1.insert ( Int_Pair ( 4, 40 ) );

   cout << "The original key values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> first;
   cout << "." << endl;

   cout << "The original mapped values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> second;
   cout << "." << endl;

   pair< map<int,int>::iterator, bool > pr;
   pr = m1.insert ( Int_Pair ( 1, 10 ) );

   if( pr.second == true ) {
      cout << "The element 10 was inserted in m1 successfully." << endl;
   }
   else {
      cout << "Key number 1 already exists in m1\n"
           << "with an associated value of ( (pr.first) -> second ) = " 
           << ( pr.first ) -> second 
           << "." << endl;
   }

   // The hint version of insert
   m1.insert( --m1.end( ), Int_Pair ( 5, 50 ) );

   cout << "After the insertions, the key values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> first;
   cout << "," << endl;

   cout << "and the mapped values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> second;
   cout << "." << endl;

   m2.insert ( Int_Pair ( 10, 100 ) );

   // The templatized version inserting a range
   m2.insert( ++m1.begin( ), --m1.end( ) );

   cout << "After the insertions, the key values of m2 =";
   for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
      cout << " " << m2_pIter -> first;
   cout << "," << endl;

   cout << "and the mapped values of m2 =";
   for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
      cout << " " << m2_pIter -> second;
   cout << "." << endl;
}
MSDN example

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧