Question

How to set all the values in a std::map to the same value, without using a loop iterating over each value?

Was it helpful?

Solution

C++ has the fill method from <algorithm> but this doesn't work for maps. In fact, algorithm support for associative containers is generally not good.

As a result, you'll have to use an interation or write an appropriate functor to be used with for_each (but I consider this to be unnecessary overhead):

template <typename TKey, typename TValue>
struct resetter : public std::unary_function<std::pair<TKey, TValue> > {
    TValue const value;

    public resetter(value const& v) : value(v) { }

    public void operator ()(std::pair<TKey, TValue>& v) {
        v.second = value;
    }
};

for_each(map.begin(), map.end(), resetter<Key, Value>(value));

OTHER TIPS

I encountered the same problem but found that the range returned by boost::adaptors::values is mutable, so it can then be used with normal algorithms such as std::fill.

#include <boost/range/adaptor/map.hpp>
auto my_values = boost::adaptors::values(my_map);
std::fill(my_values.begin(), my_values.end(), 123);

The boost::assign library has all sorts of neat stuff to help out initializing the contents of a container. My thought that this could be used to avoid explicitly iterating through the map. Unfortunately, maps are curious beasts difficult to initialize because the keys must be unique. The bottom line is that a simple for loop is probably the best way to initialize a map. It may not be super elegant, but it gets the job done and is immediatly comprehensible by anyone with any acquaintance with the STL.

map <int,string> myMap;
for( int k=0;k<1000;k++)
  myMap.insert(pair<int,string>(k,string("")));

The rest of this post describes the journey I took to reach the above conclusion.

The boost::assign makes it simple to assign a small number of values to a map.

map<string,int> m; 
insert( m )( "Bar", 1 )( "Foo", 2 );

or

 map<int,int> next = map_list_of(1,2)(2,3)(3,4)(4,5)(5,6);

In your case, where you want to initialize the entire map with the same value, there are the utilities repeat and repeat_fun.
Something like this should work with a multimap ( untested code snippet )

pair<int,string> init( 0,string(""));
multimap <int,string> myMap = repeat(1000,init);

As Konrad Rudolph as pointed out, you cannot initialize a map with the same exact value, because the keys must be unique.

This makes life much more complex ( fun? ). Something like this, perhaps:

map <int,string> myMap;

struct nextkey
{
   int start;
   nextkey( s ) : start( s ) {}
   pair<int,string> operator () ()
{
   return pair<int,string>(start++,string(""));
}
};

myMap = repeat_fun(1000,nextkey(0));

Now, this is getting so complex, I now think a simple iteration IS the way to go

map <int,string> myMap;
for( int k=0;k<1000;k++)
  myMap.insert(pair<int,string>(k,string("")));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top