简体   繁体   中英

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)

The map is declared this way:

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'

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:

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).

The other reason is that v is const which means that after it's been initialized, you can't make changes to it.

Some notes:

  • You've created both the map and the vector as 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. 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.

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 . The latter copies the data (which may be expensive) while auto& elem takes it by reference.

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 . your vector have float type. you can't push_back vector of float in one element of your vector. 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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM