0

I have a map in c++:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    map <int,int> map;
    int k;
    cin >> k;

    for (int i = 0; i < k; i++)
    {
        int m;
        cin >> m;
        map.at(m)++;
    }      
}

How can I iterate over this map and add all values of it to vector?

So map look like this:

pair<int, int>(1, 40)
pair<int, int>(22, 30)
pair<int, int>(34, 60)
pair<int, int>(41, 20)
pair<int, int>(5, 50)
pair<int, int>(6, 50)
pair<int, int>(7, 10)

I want to push the values of this to vector. So the vector will look like this: [40, 30, 60, 20, 50, 50, 10]

michel
  • 1
  • 1
  • A vector of *what*? What is the data that you want to add to the vector? What have you tried yourself? What problems do you have with your own attempt? Also please take some time to read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 10 '20 at 09:09
  • Check now the ask – michel Apr 10 '20 at 09:11
  • On another couple of notes, the code you currently show won't work because the [`at`](https://en.cppreference.com/w/cpp/container/map/at) function will not add new elements to the map. Instead it will throw an exception. Also please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Apr 10 '20 at 09:12
  • It does not compile `bez nazwy.cpp:17:50: error: invalid initialization of non-const reference of type '__gnu_cxx::__normal_iterator >&' from an rvalue of type 'std::vector::iterator {aka __gnu_cxx::__normal_iterator >}'` – michel Apr 10 '20 at 09:17
  • `std::map` is sorted by the key, and in your example pairs are not sorted w.r.t the first element. Does the order of elements in the resulting vector matter? – Evg Apr 10 '20 at 09:18
  • #include #include #include int main() { std::map mapData{ {1,40},{22,30},{34,60},{41,20},{5,50},{6,50},{7,10}}; std::vector values{}; for (const auto& [key, value] : mapData) values.push_back(value); return 0; } – A M Apr 10 '20 at 09:22
  • In case you want to do is to add all map values to a new vector you can do: `map m; // The original map vector v; // The destination vector for(map::iterator it = m.begin(); it != m.end(); ++it) { v.push_back(it->second); }` – arielhad Apr 10 '20 at 09:22
  • The order does not matter – michel Apr 10 '20 at 09:40

2 Answers2

1

Using std::copy is not the best idea because the value_type of the map is std::pair<const int, int> and of the vector is simply int, so I will use std::for_each which allows you to copy in your specific way.

std::for_each.

Example:

std::map<int, int> map;
std::vector<int> vec;
...
std::for_each(map.begin(), map.end(), [&vec](const auto pair) {
    vec.push_back(pair.second);
});

std::transform

std::transform(map.begin(), map.end(), std::back_inserter(vec), [](const auto pair) { return pair.second; });
Michael
  • 932
  • 6
  • 15
1

Here you are.

#include <iostream>
#include <vector>
#include <map>

std::vector<int> map_to_vector( const std::map<int, int> &m )
{
    std::vector<int> v;
    v.reserve( m.size() );

    for ( const auto &p : m )
    {
        v.push_back( p.second );
    }

    return v;
}

int main() 
{
    std::map<int, int> m =
    {
        { 1, 40 }, { 22, 30 }, { 34, 60 }, { 41, 20 }, { 5, 50 }, { 6, 50 }, { 7, 10 }
    };

    auto v = map_to_vector( m );

    for ( const auto &item : v )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';

    return 0;
}

The program output is

40 50 50 10 30 60 20 

Payb attention that the map is ordered by its key. So the vector will contain values in the order according to the order of keys in the map.

Or as it was written by @Jerry Jeremiah in a comment you can use the standard algorithm std::transform.

std::vector<int> map_to_vector( const std::map<int, int> &m )
{
    std::vector<int> v;
    v.reserve( m.size() );

    std::transform( std::begin( m ), std::end( m ), std::back_inserter( v ),
                    []( const auto &p ) { return p.second; } );
    return v;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335