我有以下两个变量:
static vector<map<uint32_t,vector<uint64_t>>> relations;
static map<uint64_t,map<uint32_t,vector<uint64_t>>> transactions;
uint64_t key1;
uint32_t key2;
uint32_t key3;
// init keys...
我正在尝试将vector
中的一个relations
复制到transactions
:
transactions[key1][key2].push_back(relations[key2][key3]));
但是我发现了这个错误:
main.cpp:175:26: error: no matching member function for call to 'push_back'
transactions[key1][key2].push_back(relations[key2][key3]));
~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:700:36: note: candidate function not viable: no
known conversion from 'vector<uint64_t>' to 'const value_type' (aka 'const unsigned long long') for 1st argument
_LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:702:36: note: candidate function not viable: no
known conversion from 'vector<uint64_t>' to 'value_type' (aka 'unsigned long long') for 1st argument
_LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
知道发生了什么?
答案 0 :(得分:6)
relations[key2][key3]
是vector<uint64_t>
,您尝试将其添加到transactions[key1][key2]
vector<uint64_t>
。
您无法将vector<uint64_t>
添加到vector<uint64_t>
。
如果您要将第一个vector
的内容添加到第二个{em>内容,则需要使用std::vector::insert
:
auto& dst = transactions[key1][key2];
const auto& src = relations[key2][key3];
dst.insert(dst.end(), src.begin(), src.end());