[英]C++ - copying vector in foreach gives "No matching function to call for std::vector<int>::push_back(std::vector<int>&)"
I have the following function:我有以下功能:
std::vector<std::vector<int>> solve(int t){
std::vector<std::vector<int>> result;
result.push_back(std::vector<int>(2*t,0));
//CODE TO fill up result[0]
return result;
}
And when I write the following code to get results:当我编写以下代码以获得结果时:
std::vector<std::vector<int>> results(4);
for(int t = 0; t < 4; ++t){
std::vector<std::vector<int>> cols = solve(t);
if(cols.size() > 0){
for(std::vector<int> col: cols){
results[t].push_back(col);
}
}
}
I get the following error:我收到以下错误:
src/pricing.cpp:33:29: error: no matching function for call to ‘std::vector<int>::push_back(std::vector<int>&)’
results[t].push_back(col);
From what I understand the range based for is creating col
as a reference.据我了解,基于的范围是创建
col
作为参考。 What I don't understand is push_back
being able to insert col
.我不明白的是
push_back
能够插入col
。 Why is this happening and what's the best way to insert col
into results[t]
?为什么会发生这种情况,将
col
插入results[t]
的最佳方法是什么?
col
is a vector<int>
. col
是一个vector<int>
。
You're attempting to add that to an element of results
, which can only hold int
s.您正在尝试将其添加到
results
元素中,该元素只能保存int
s。
That's what the compiler is telling you.这就是编译器告诉你的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.