Iterator for C++ 11 map值(简单透明)

kdo*_*dog 9 c++ iterator std map c++11

我正在寻找一种简单的方法来为mapC++ 11中的a值创建一个迭代器.

这个方法应该简单透明:简单,因为它应该易于实现,并且透明,因为客户端不应该知道值来自地图而不是集合.

之前已经多次询问过这个问题.其中许多问题早于C++ 11并使用boost,我不想使用它.有些并不简单,John Ahlgren的解决方案,例如http://john-ahlgren.blogspot.com/2013/10/how-to-iterate-over-values-of-stdmap.html,例如需要一页代码编写自定义迭代器.

其他人不透明,即显然可以写:

map<string,foo> mymap;
for (auto it=mymap.begin();it!=mymap.end();++it){
  Foo val= it->second;
  ...
 }
Run Code Online (Sandbox Code Playgroud)

但是,我不想这样做,因为我不希望客户端必须知道数据表示.

问题出现如下.

我有一堆用长"钥匙"唯一索引的对象.有时我想操纵这些对象的集合.其他时候我想要检索一个给定键的对象.

我不能直接使用"set"类有几个原因,其中主要是它不存储可变实例,并且这些实例必须是可变的(显然,除了键之外).

所以,我决定将所有对象存储在一个巨大的全局哈希表中:

map<long,Foo> all_the_objects;
Run Code Online (Sandbox Code Playgroud)

然后我根本不工作set<Foo>.相反,我使用set<long>并使用适配器来模拟一组Foo,即

class SetOfFoo{
  private: set<long> theKeys;
  public:
    void insert(const & Foo);
    size_t size() return theKeys.size();
    bool is_member(const & Foo)
      {return theKeys.find(Foo.key)
          != theKeys.end;}
    Foo & insert(const & Foo val){
       long key=val.key;
       all_the_objects[key]=val;
       return all_the_objects[key];
     }
    ...::iterator begin() {???}
}
Run Code Online (Sandbox Code Playgroud)

换句话说,SetOfFoo类的客户端不知道或不需要知道SetOfFoo是作为一组键实现的.

我也不能在适配器类中自己创建一个Vector,因为无法在C++集合中存储引用.

是否真的不可能以简单,透明的方式迭代map <>值?我觉得很难相信,因为这是一个非常普遍的需求,并且在我看到的每种语言中都有微不足道的事情.我只是不明白这怎么可能很难.

Ric*_*ges 7

这是非常微不足道的.

这是一个非常简单的版本,它最小化地解决了整数字符串映射的问题.您可以根据需要重写所需的类型,也可以根据需要进行模板化.

#include <map>
#include <iostream>
#include <iterator>
#include <string>
#include <algorithm>

struct map_value_iterator : public std::map<int, std::string>::const_iterator
{
    map_value_iterator(std::map<int, std::string>::const_iterator src)
    : std::map<int, std::string>::const_iterator(std::move(src))
    {

    }

    // override the indirection operator
    const std::string& operator*() const {
        return std::map<int, std::string>::const_iterator::operator*().second;
    }
};


using namespace std;


int main()
{
    map<int, string> myMap { {1, "Hello" }, { 2, "World" } };

    copy(map_value_iterator(begin(myMap)), map_value_iterator(end(myMap)), ostream_iterator<string>(cout , " "));
    cout << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

节目输出:

Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1

Executing the program....
$demo 
Hello World 
Run Code Online (Sandbox Code Playgroud)