gpt4 book ai didi

c++ - 如何将结构传递给 STL 映射?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:05:04 27 4
gpt4 key购买 nike

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

最佳答案

您需要将值插入 map :

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

有关插入 map 的其他方法,请参阅 map::insert() reference page .

关于c++ - 如何将结构传递给 STL 映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8801345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com