8

如何在std::map不使用循环遍历每个值的情况下将 a 中的所有值设置为相同的值?

4

3 回答 3

17

使用循环迄今为止最简单的方法。事实上,它是单行的:[C++17]

for (auto& [_, v] : mymap) v = value;

不幸的是,对关联容器的 C++ 算法支持在 C++20 之前并不是很好。因此,我们不能直接使用std::fill.

无论如何要使用它们(C++20 之前),我们需要编写适配器——在 的情况下std::fill,是一个迭代器适配器。这是一个最小可行(但不是真正符合)的实现来说明这是多少努力。我建议按原样使用它。使用库(例如Boost.Iterator)进行更通用的生产强度实现。

template <typename M>
struct value_iter : std::iterator<std::bidirectional_iterator_tag, typename M::mapped_type> {
    using base_type = std::iterator<std::bidirectional_iterator_tag, typename M::mapped_type>;
    using underlying = typename M::iterator;
    using typename base_type::value_type;
    using typename base_type::reference;

    value_iter(underlying i) : i(i) {}

    value_iter& operator++() {
        ++i;
        return *this;
    }

    value_iter operator++(int) {
        auto copy = *this;
        i++;
        return copy;
    }

    reference operator*() { return i->second; }

    bool operator ==(value_iter other) const { return i == other.i; }
    bool operator !=(value_iter other) const { return i != other.i; }

private:
    underlying i;
};

template <typename M>
auto value_begin(M& map) { return value_iter<M>(map.begin()); }

template <typename M>
auto value_end(M& map) { return value_iter<M>(map.end()); }

有了这个,我们可以使用std::fill

std::fill(value_begin(mymap), value_end(mymap), value);
于 2008-09-26T13:06:21.853 回答
3

我遇到了同样的问题,但发现 boost::adaptors::values 返回的范围是可变的,因此它可以与 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);
于 2015-06-22T05:54:02.747 回答
2

boost::assign 库有各种简洁的东西来帮助初始化容器的内容。我认为这可以用来避免显式地遍历地图。不幸的是,地图是难以初始化的奇怪野兽,因为键必须是唯一的。底线是简单的 for 循环可能是初始化地图的最佳方式。它可能不是超级优雅,但它完成了工作,并且任何熟悉 STL 的人都可以立即理解。

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

这篇文章的其余部分描述了我为达到上述结论所经历的旅程。

boost::assign 使将少量值分配给映射变得简单。

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

或者

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

在您的情况下,您想用相同的值初始化整个地图,有实用程序repeat 和repeat_fun。
像这样的东西应该适用于多图(未经测试的代码片段)

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

正如 Konrad Rudolph 所指出的,您不能使用相同的确切值初始化地图,因为键必须是唯一的。

这使生活变得更加复杂(有趣吗?)。像这样的东西,也许:

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

现在,这变得如此复杂,我现在认为一个简单的迭代是要走的路

map <int,string> myMap;
for( int k=0;k<1000;k++)
  myMap.insert(pair<int,string>(k,string("")));
于 2008-09-26T13:42:04.590 回答