如何将结构体传递给STL map?

6
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**)’

3
注意,使用 typedef 来为结构体命名在 C++ 中是无用的。 - log0
3
建议使用 std::string 代替 char*。如果你为 message 进行动态内存分配,需要为 threadData 编写复制构造函数、赋值运算符和析构函数。 - hmjd
1
有人在混用C和C++... - Lightness Races in Orbit
1
返回翻译后的文本:并且-1表示明显没有阅读std::map::insert文档。 - Lightness Races in Orbit
1
@Anisha:在C++中,9.1/2([class.name])描述了如何引用用户定义类型,在世界上几乎每一本书或教程中的C++代码示例都只显示类名的使用;-)我想用C++和C之间的差异来描述它并不常见,大多数作者只是描述C++的行为。 - Steve Jessop
显示剩余3条评论
4个回答

8

您需要将值插入到映射中:

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

如果想要了解其他向映射表中插入元素的方法,请查看map::insert()参考页面


这意味着传递除了常量如1、2等之外的任何内容都需要成对出现吗? - Aquarius_Girl
1
@AnishaKaul 不需要。Map的值是一对。当向Map中插入元素时,如果使用我链接页面上的第一个插入,则需要插入一对。如果使用第二个或第三个插入,则不需要一对。 - BЈовић
在该链接中展示了插入元素的两种方式。当您需要在一个位置插入两个东西时,需要使用Pair。我的结构是一个对象,不确定为什么它需要一对? - Aquarius_Girl
1
@AnishaKaul 实际上,该链接展示了向映射插入元素的三种方式。映射的值的类型为std::pair<key, value>。当使用第一种插入方式时,您需要创建一个pair,其中第一个元素是键,第二个元素是值(在您的示例中是对象)。 - BЈовић
是的,我意识到了我的错误,我在“想象”这个语法:x.insert(11, 12);。谢谢。 - Aquarius_Girl

3
< p >对于insert函数,您需要一对值,因为与其他容器一样,insert需要value_type,在map的情况下,每个条目都有一个键和一个映射值。 value_type表示容器中的一个条目,因此包括两者。

如果您喜欢这种方式,可以编写serverPortNumberThreadId[3490] = obj;。行为有时会有所不同(如果键已经存在,则insert什么也不做,而此代码会覆盖它,但除非您已经依赖于insert的这种行为,否则这对您没有影响)。在创建/复制/分配threadData对象的数量方面,性能可能略有不同。


3
 //always overwrites the old value
 serverPortNumberThreadId[3490]=obj;
 //only insert a new one
 serverPortNumberThreadId.insert(std::make_pair(3490, obj));

1

你需要发送一个pair到插入函数,而不是key,value

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接