繁体   English   中英

在 std::map 中设置所有值

[英]Setting all values in a std::map

如何将std::map中的所有值设置为相同的值,而不使用循环遍历每个值?

使用循环迄今为止最简单的方法。 事实上,它是单行的: [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);

我遇到了同样的问题,但发现 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);

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

正如康拉德鲁道夫所指出的那样,您不能使用相同的确切值初始化映射,因为键必须是唯一的。

这让生活变得更加复杂(有趣?)。 像这样的事情,也许:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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