c++STL之multimap

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/kaikai_sk/article/details/78123349
#include <iostream>
#include <map>
#include<functional>
using namespace std;

/*
   multimap多重映照容器:容器的数据结构采用红黑树进行管理
   multimap的所有元素都是pair:第一元素为键值(key),不能修改;第二元素为实值(value),可被修改

   multimap特性以及用法与map完全相同,唯一的差别在于:
   允许重复键值的元素插入容器(使用了RB-Tree的insert_equal函数) 
*/
int main()
{
    //greater<float> :按key值由大到小排序
    multimap<float, char*, greater<float> > mm;

    mm.insert(pair<float, char*>(3.0f, "apple"));
    mm.insert(pair<float, char*>(3.0f, "pear"));
    mm.insert(pair<float, char*>(2.1f, "orange"));
    mm.insert(pair<float, char*>(1.5f, "banana"));

    multimap<float, char*>::iterator begin, end;
    end = mm.end();
    for (begin = mm.begin(); begin != end; begin++)
    {
        cout << (*begin).second << "\t" << (*begin).first << "元/斤" << endl;
    }
    cout << endl;
    cin.get();
    return EXIT_SUCCESS;
}
展开阅读全文

STL::multimap 的使用问题

10-16

[code=C/C++]rn#pragma warning(disable: 4786)rn#pragma warning(disable: 4096)rnrn#include rn#include rn#include rn#include rnusing namespace std;rnrn#include "afxmt.h"rnrntypedef multimap mmss;rntypedef multimap ::const_iterator CIT; rntypedef multimap ::iterator IT; rntypedef pair Range;rnrnvoid main()rn rn multimaphello;rn hello.insert(make_pair(2,3));rnrn multimap efg;rn efg.insert(make_pair("3", "4"));rnrn multimap DNS_daemon;rn rn DNS_daemon.insert(make_pair("213.108.96.7","cppzone.com"));rn DNS_daemon.insert(make_pair("213.108.96.7","cppluspluszone.com"));rn cout<second).c_str()<< endl; rnrn DNS_daemon.insert(make_pair("219.108.96.70", "pythonzone.com"));rn CIT cit=DNS_daemon.upper_bound("213.108.96.7");rn if (cit!=DNS_daemon.end()) //found anything?rn cout<<(cit->second).c_str()<second).c_str()< &,const class std::multimap<_K,_Ty,_Pr,_A> &)' : could not deduce template argument for 'constrn class std::multimap<_K,_Ty,_Pr,_A> &' from 'const class std::basic_string,class std::allocator >'rn c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less,class std::allocator > >::operarntor ()(const class std::basic_string,class std::allocator > &,const class std::basic_string,class std::allocator > &) const'rnc:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::map<_K,_Ty,_Pr,_A> &,const class std::map<_K,_Ty,_Pr,_A> &)' : could not deduce template argument for 'const class stdrn::map<_K,_Ty,_Pr,_A> &' from 'const class std::basic_string,class std::allocator >'rn c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less,class std::allocator > >::operarntor ()(const class std::basic_string,class std::allocator > &,const class std::basic_string,class std::allocator > &) const'rnc:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::_Tree<_K,_Ty,_Kfn,_Pr,_A> &,const class std::_Tree<_K,_Ty,_Kfn,_Pr,_A> &)' : could not deduce template argument for 'crnonst class std::_Tree<_K,_Ty,_Kfn,_Pr,_A> &' from 'const class std::basic_string,class std::allocator >'rn c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less,class std::allocator > >::operarntor ()(const class std::basic_string,class std::allocator > &,const class std::basic_string,class std::allocator > &) const'rnc:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce ternmplate argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &' from 'const class std::basic_string,class std::allocator >'rn c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less,class std::allocator > >::operarntor ()(const class std::basic_string,class std::allocator > &,const class std::basic_string,class std::allocator > &) const'rnc:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator <(const struct std::pair<_T1,_T2> &,const struct std::pair<_T1,_T2> &)' : could not deduce template argument for 'const struct std::pair &' from 'const class std::basic_string,class std::allocator >'rn c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less,class std::allocator > >::operarntor ()(const class std::basic_string,class std::allocator > &,const class std::basic_string,class std::allocator > &) const'rnc:\program files\microsoft visual studio\vc98\include\functional(86) : error C2676: binary '<' : 'const class std::basic_string,class std::allocator >' does not define this operator or a conversion to a type rnacceptable to the predefined operatorrn c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less,class std::allocator > >::operarntor ()(const class std::basic_string,class std::allocator > &,const class std::basic_string,class std::allocator > &) const'rnError executing cl.exe.rnrnEX32.exe - 6 error(s), 0 warning(s)rnrnrn大家帮我看看怎么解决这个问题。 论坛

求解答:关于STL multimap赋值问题

07-01

程序如下:想用multimap来存放自己定义的一个类,可是无法用下标进行赋值。完整代 rn码如下(vs2008),有没有遇到过。 A的拷贝构造,赋值运算符,还有比较运算符定不定义,错误都是一样的。rnC/C++ codern#include rn#include rn#include rnusing namespace std; rnrn[code=C/C++]rnclass A rn rnpublic: rn A(const string& _name, int _no) : rn name(_name), no(_no) rn rn rn A() rn A(const A& aa) rn rn name = aa.name; rn no = aa.no; rn rnrn A& operator=(const A& aa) rn rn name = aa.name; rn no = aa.no; rn return *this; rn rn friend bool operator<(A& a, A& aa); rn friend bool operator==(A& a, A& aa); rnrn rnprivate: rn string name; rn int no; rn; rnrn bool operator<( A& a, A& aa) rn rn return a.name < aa.name; rn rnrn bool operator==( A&a, A& aa) rn rn if ( a.name == aa.name ) rn return true; rn else rn return false; rn rnrnvoid main() rn rn A s("silver",10001); rn A s2("silver",10002); rn std::multimap m; rn //cout << (s == s2 )<< endl; rn m.insert(make_pair("silver",s)); rn //m.insert(make_pair("silver",s2)); rn m["silver"] = s2; rn rn[/code]rnrnrn错误信息:d:\我的文档\visual studio 2008\projects\testmultimap\main.cpp(70) : rn error C2676: 二进制“[”: “std::multimap<_Kty,_Ty>”不定义该运算符或到预定 rn义运算符可接收的类型的转换 rn1> with rn1> [ rn1> _Kty=std::string, rn1> _Ty=A rn1> ] rn1>生成日志保存在“file://d:\我的文档\Visual Studio 2008\Projects\ rnTestMultimap\Debug\BuildLog.htm” rn1>TestMultimap - 1 个错误,0 个警告 rn========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ========== 论坛

没有更多推荐了,返回首页