[英]error: no matching function for call to 'std::vector<float>::push_back(std::vector<float>&) const'
I am trying to copy map (m) values into a vector (v)我正在尝试将 map (m) 值复制到向量 (v) 中
The map is declared this way: map 是这样声明的:
const map<int, vector<float> >& m = ....;
I tried this:我试过这个:
const vector<float>& v;
for(auto elem : m)
v.push_back(elem.second);
I get this error:我收到此错误:
error: no matching function for call to 'std::vector::push_back(std::vector&) const'错误:没有匹配的 function 调用 'std::vector::push_back(std::vector&) const'
The purpose of what you are doing is a little unclear but if you'd like to make a vector<float>
to hold all the values in all the vectors in the map
, you could do that, but you can't push_back
a vector<float>
directly, which is one of the reasons why you get the error:你所做的事情的目的有点不清楚,但如果你想制作一个
vector<float>
来保存map
中所有向量中的所有值,你可以这样做,但你不能push_back
一个vector<float>
直接,这是您收到错误的原因之一:
error: no matching function for call to 'std::vector::push_back(std::vector&) const'
You can either push_back
the float
values one-by-one - or use the vector::insert
member function to insert all of the elements in one go (which it looks like you are trying to do).您可以将
float
值一一推回 - 或使用vector::insert
成员push_back
将所有元素插入一个 go (看起来您正在尝试这样做)。
The other reason is that v
is const
which means that after it's been initialized, you can't make changes to it.另一个原因是
v
是const
,这意味着在它被初始化后,您不能对其进行更改。
Some notes:一些注意事项:
map
and the vector
as const&
s.map
和vector
作为const&
s。 This works and extends the lifetime of the temporary map
and vector
that you create this way - but being const
you can't change any of them.map
和您以这种方式创建的vector
的生命周期 - 但作为const
您无法更改其中任何一个。 I suggest that you remove the reference on at least the vector
and also make it non- const
to be able to fill it up.vector
上的引用,并使其成为非const
以便能够填充它。 Example:例子:
#include <map>
#include <vector>
int main() {
const std::map<int, std::vector<float>> m { {1, {2,3,4}} };
std::vector<float> v;
for(auto& elem : m) {
// insert the vector values from this map-entry at the end of `v`
v.insert(v.end(), elem.second.begin(), elem.second.end());
}
}
Note auto& elem
instead of auto elem
.注意
auto& elem
而不是auto elem
。 The latter copies the data (which may be expensive) while auto& elem
takes it by reference.后者复制数据(这可能很昂贵),而
auto& elem
通过引用获取数据。
Using structured bindings, the filling loop would look like this:使用结构化绑定,填充循环将如下所示:
for(auto&[first, second] : m) {
v.insert(v.end(), second.begin(), second.end());
}
your map type is: int, vector<float>
it means your values are type of vector
of floats
.你的 map 类型是:
int, vector<float>
这意味着你的值是floats
的vector
类型。 your vector have float type.你的向量有浮点类型。 you can't push_back vector of float in one element of your vector.
您不能在向量的一个元素中 push_back 向量的浮点数。 you need to do this.
你需要这样做。
vector<float> v;
for(auto elem : m)
{
for(auto f : elem.second)
v.push_back(f);
}
or something like that depends on how you want to use your values.或类似的事情取决于你想如何使用你的价值观。
You're trying to push a vector to a vector, which doesn't make sense.您正在尝试将向量推送到向量,这是没有意义的。 An object can't be an element of itself.
object 不能是其自身的元素。
Change it to:将其更改为:
const map<int, float>& m = ....;
Or或者
for(auto elem : m)
{
int i = 0;
while(i < elem.second.size())
{
v.push_back(elem.second[i]);
i += 1;
}
}
you try to push VECTOR to vector u need Vector of Vector for this to work your element is vector and variable v is a vector u need vector<vector>你尝试将 VECTOR 推到向量你需要 Vector of Vector 才能工作你的元素是向量,变量 v 是一个向量你需要 vector<vector>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.