【问题标题】:adding string and list to map in c++在 C++ 中添加字符串和列表以映射
【发布时间】:2023-04-07 23:35:02
【问题描述】:

我是 C++ 新手。我正在尝试在 cpp 中添加龙对象,但在添加对象时收到错误

#include "dragon.h"
using std::cin;
#include <map>
using std::map;
#include <list>
#include <string>
#include <iostream>
using namespace std;
using std::list;

typedef map<string,list<Dragon> >::iterator iter;
typedef map<const string,list<Dragon> >::value_type dmaptype;
void display(map<string,list<Dragon> > dmap1,iter dmapiter1);


int main( )
{
  map<string,list<Dragon> >dmap;
iter dmapiter;

bool again = true;

string name;
double length;
string colour;
string location;
while( again )
{
     // get the details for the dragon
     cout << "\nEnter dragon name >> ";
     getline( cin, name );

     cout << "\nEnter dragon length >> ";
     cin >> length;

     cin.get( );

     cout << "\nEnter dragon colour >> ";
     getline( cin, colour );

     // now get the location
     cout << "\nEnter location >> ";
     getline( cin, location );
     dmapiter=dmap.find(location);

    Dragon * ptr ;
    ptr=new Dragon(name,length,colour);

     if(dmapiter==dmap.end())
     {
        list<Dragon*> dlist;
        dlist.push_back(ptr);
        dmap.insert(dmaptype(location, dlist));
     }
     else
     {
        dmapiter->second.push_back(*ptr);
     }
     char choice;

     cout << "\nEnter another dragon and location [ y / n ] ? ";
     cin >> choice;
     cin.get( );
     if( choice != 'y' && choice != 'Y' )
     {
          again = false;
     }

}
 display(dmap,dmapiter);


cout << endl;

return 0;


}

当我编译程序时,我收到一个错误:

dmap.insert(dmaptype(location, dlist));

错误是:

 error: no matching function for call to ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::list<Dragon, std::allocator<Dragon> > >::pair(std::string&, std::list<Dragon*, std::allocator<Dragon*> >&)’
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:83: note: candidates are: std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = std::list<Dragon, std::allocator<Dragon> >]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:79: note:                 std::pair<_T1, _T2>::pair() [with _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = std::list<Dragon, std::allocator<Dragon> >]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:68: note:                 std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::list<Dragon, std::allocator<Dragon> > >::pair(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::list<Dragon, std::allocator<Dragon> > >&)

任何帮助将不胜感激...

谢谢

【问题讨论】:

    标签: c++ string list map


    【解决方案1】:

    试试:

    dmap.insert(std::make_pair(location, dlist));
    

    但您还需要使 dlist 的类型与您的地图的类型一致 - 两者都使用 list&lt;Dragon&gt;list&lt;Dragon*&gt;

    为简单起见(因此您不必担心删除),请先尝试list&lt;Dragon&gt;。 当你插入到这个列表中时,会创建一个副本(因为 STL 具有值语义),所以如果你在堆栈上本地创建一个 Dragon 以插入到 dlist 中是可以的。 (您可以将其视为正在制作的副本:如果编译器很聪明,则可能会发生复制省略,但暂时不要担心)。

    例如:

    Dragon dragon(name,length,colour);
    
    if(dmapiter==dmap.end())
    {
      list<Dragon> dlist;
      dlist.push_back(dragon);
      dmap.insert(make_pair(location, dlist));
    }
    else
    {
      dmapiter->second.push_back(dragon);
    }
    

    【讨论】:

    • 您好,它给出了同样的错误......该项目的要求是使用地图并尝试将龙对象列表添加到其中,并将键作为字符串。
    • 您是否将您的 dlist 修改为 list 并摆脱新的等...? list 与 list 不同。
    • 看看我添加到答案中的 sn-p - 你的错误是假设 list 与 list 相同 - 它们是不同的类型。
    猜你喜欢
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2021-08-15
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多