【问题标题】:How to pass a structure to a STL map?如何将结构传递给 STL 映射?
【发布时间】:2012-02-06 18:01:07
【问题描述】:
typedef struct
{
    pthread_t threadId;
    int       acceptSocketD;
    char      *message;
} threadData;

map <unsigned int, threadData> serverPortNumberThreadId;
map <unsigned int, threadData> :: iterator serverPortNumberThreadIdIter;

用法:

threadData obj; 
obj.threadId      = 0;
obj.acceptSocketD = 0;
obj.message       = "Excuse Me, please!";

serverPortNumberThreadId.insert (3490, obj);

错误:

error: no matching function for call to ‘std::map<unsigned int, threadData>::insert(int, threadData&)’
/usr/include/c++/4.5/bits/stl_map.h:500:7: note: candidates are: std::pair<typename std::map<_Key, _Tp, _Compare, _Alloc>::_Rep_type::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const std::map<_Key, _Tp, _Compare, _Alloc>::value_type&) [with _Key = unsigned int, _Tp = threadData, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, threadData> >, typename std::map<_Key, _Tp, _Compare, _Alloc>::_Rep_type::iterator = std::_Rb_tree_iterator<std::pair<const unsigned int, threadData> >, std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const unsigned int, threadData>]
/usr/include/c++/4.5/bits/stl_map.h:540:7: note:                 std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::iterator, const std::map<_Key, _Tp, _Compare, _Alloc>::value_type&) [with _Key = unsigned int, _Tp = threadData, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, threadData> >, std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const unsigned int, threadData> >, std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const unsigned int, threadData>]
tcpClient.cpp: In function ‘int main(int, char**)’

【问题讨论】:

  • 只说一句,在C++中使用typedef命名结构是没用的。
  • 考虑使用std::string 而不是char*。如果为message动态分配内存,则需要为threadData编写拷贝构造函数、赋值运算符和析构函数。
  • 有人在混合 C 和 C++...
  • 和 -1 表示显然不想阅读 std::map::insert 的文档
  • @Anisha:通常在 C++ 中,您只需编写 struct threadData { ... };。在 C 中,如果你这样做,那么要引用你必须使用的类型struct threadData。然而,即使没有 typedef,C++ 也允许您将类型称为 threadData

标签: c++ stl map struct


【解决方案1】:

您需要将值插入到地图中:

serverPortNumberThreadId.insert ( std::make_pair( 3490, obj) );

关于插入地图的其他方式,请参阅map::insert() reference page

【讨论】:

  • 这意味着传递任何超出常量(如 1,2 等)的东西都需要一对?
  • @AnishaKaul 否。地图的值是一对。将元素插入地图时,如果您使用我链接的页面中的第一个插入,则需要插入一对。如果您使用第 2 个或第 3 个插入物,则不需要一对。
  • 并在该链接中显示了两种插入元素的方式。当您需要插入两件东西代替一件时,需要配对。我的结构是一个对象,不知道为什么需要一对?
  • @AnishaKaul 实际上,该链接显示了将元素插入地图的 3 种方式。映射的值是 std::pair 类型。使用第一个插入时,您需要创建一个对,其中第一个元素在键中,第二个是值(示例中的对象)
  • 是的,我意识到我的错,我在“想象”这个语法:x.insert (11, 12);。谢谢。
【解决方案2】:

insert 函数需要一对,因为与其他容器一样,insert 采用 value_type,在 map 的情况下是一对——每个条目都有一个键和一个映射价值。 value_type 代表容器中的一个条目,因此包括两者。

如果您更喜欢它的外观,您可以改写 serverPortNumberThreadId[3490] = obj;。行为有时会有所不同(如果密钥已经存在,insert 什么也不做,而这段代码会覆盖它,但除非您依赖 insert 的行为,否则这对您没有任何影响)。就创建/复制/分配的threadData 对象的数量而言,性能可能略有不同。

【讨论】:

  • 该死!该死的我现在意识到我的错了!!!傻我。也许在梦中我看到了 map 的用法,例如:x.insert (11, 12); 嗯,这个问题值得反对。
【解决方案3】:
 //always overwrites the old value
 serverPortNumberThreadId[3490]=obj;
 //only insert a new one
 serverPortNumberThreadId.insert(std::make_pair(3490, obj));

【讨论】:

  • 请看我对 VJovic 回答的最后评论。,
  • @AnishaKaul 您的回答需要更多细节。太好了。
【解决方案4】:

您必须发送 pair 来插入函数,而不是 key,value

mymap.insert ( pair<unsigned int,threadData>(3490, obj) );

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多