简体   繁体   English

错误:没有匹配的 function 调用 'std::vector<float> ::push_back(std::vector<float> &amp;) 常量'</float></float>

[英]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.另一个原因是vconst ,这意味着在它被初始化后,您不能对其进行更改。

Some notes:一些注意事项:

  • You've created both the map and the vector as const& s.您已经创建了mapvector作为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>这意味着你的值是floatsvector类型。 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.

相关问题 没有用于调用std :: vector的匹配函数 <std::tuple> 推回 - no matching function for call to std::vector<std::tuple> push_back 没有用于调用 &#39;std::vector 的匹配函数<node*> ::push_back(节点*&amp;)&#39; - no matching function for call to ‘std::vector<node*>::push_back(Node*&)’ 没有匹配的函数调用&#39;std::vector &gt;::push_back(char&amp;)&#39; - no matching function for call to 'std::vector >::push_back(char&)' 没有匹配的 function 调用 'std::vector <std::vector<int> &gt;::push_back(int)' </std::vector<int> - no matching function for call to 'std::vector<std::vector<int> >::push_back(int)' 错误:没有匹配的函数调用&#39;std :: vector <std::vector<int> &gt; ::的push_back(标准::矢量 <std::__cxx11::basic_string<char> &gt;&)” - error: no matching function for call to ‘std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string<char> >&)’ 错误:没有匹配的函数来调用“std::vector”<x> ::push_back(y&amp;) 在 C++ - error: no matching function for call to ‘std::vector<x>::push_back(y&) in C++ 填充向量的向量时出错:没有匹配的函数来调用std :: basic_string :: push_back - Error populating a vector of vectors: no matching function to call for std::basic_string::push_back 没有匹配的 function 调用 'std::vector::push_back(std::string&amp;)' - No matching function for call to ‘std::vector::push_back(std::string&)’ 没有匹配的函数来调用 std::vector<float> - No matching function to call to std::vector<float> 错误:没有匹配函数来调用&#39;std :: vector <std::__cxx11::basic_string<char> &gt; ::的push_back(INT&)” - error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(int&)’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM