简体   繁体   English

std :: map中向量值的迭代器

[英]Iterator for vector values in a std::map

I have a class containing a map that stores vectors of shared_ptr objects. 我有一个包含map的类,该map存储了shared_ptr对象的向量。

#include <map>
#include <memory>
#include <vector>

template <class T, class U>
class MyMap {
public:
    typedef std::shared_ptr<U> UPtr;
    typedef std::vector<UPtr> UPtrVec;
    typedef std::map<T, UPtrVec> VecMap;
    ...
private:
    VecMap vec_map;
};

Is it possible to create an iterator (using boost, I presume) for the values inside each mapped vector value? 是否可以为每个映射的矢量值内的值创建一个迭代器(使用boost,我想)? I would also like to be able to iterate over the stored values for a specified list of keys. 我还希望能够遍历指定键列表的存储值。

For example, if my map contained data like this (switching to Python syntax for brevity) 例如,如果我的地图包含这样的数据(为了简洁起见,请切换到Python语法)

 // note: in actuality the vector contents are shared_ptr's to objects
 vec_map[5] = ["one", "two", "three"]
 vec_map[8] = ["four", "five"]
 vec_map[3] = ["six", "seven", "eight", "nine"]

Is it possible to write an iterator interface such that I could do the following: 是否可以编写一个迭代器接口,使我可以执行以下操作:

MyMap<int, std::string> mymap
...
for(auto it = mymap.begin(), it != mymap.end(); ++it) 
    cout << *it << " ";

and have it output "one two three four five six seven eight nine" ? 并输出“一二三四五五六七八九”吗? If I could get that to work, I'd like to write an iterator that would filter based on key values. 如果我可以使用它,我想编写一个迭代器,该迭代器将根据键值进行过滤。

Barring that I was think about creating another UPtrVec in the class that stored all UPtr objects in a flat list. 除非我考虑过UPtrVec在将所有UPtr对象存储在平面列表中的类中创建另一个UPtrVec But I lose the mapping that would allow me to filter based on key values, unless I embed the key info into the U objects. 但是,除非将密钥信息嵌入到U对象中,否则我将丢失允许我根据密钥值进行过滤的映射。

You can create your own iterator just using standard C++: 您可以仅使用标准C ++创建自己的迭代器:

Create a class and inherit if iterator class using one of the standard iterator tag ( input_iterator_tag , output_iterator_tag , forward_iterator_tag , bidirectional_iterator_tag , random_access_iterator_tag ) depending the type of iterator you want. 创建一个类,如果继承iterator使用标准的iterator标签(一类input_iterator_tagoutput_iterator_tagforward_iterator_tagbidirectional_iterator_tagrandom_access_iterator_tag这取决于你想迭代器的类型)。 ( input_iterator_tag is enough for the sample you show). (对于您显示的示例, input_iterator_tag就足够了)。

Implement in that class the iterator interface required by the type. 在该类中实现类型所需的迭代器接口。 (In cppreference.com you can found the requirements for each iterator). (在cppreference.com中,您可以找到每个迭代器的要求)。

class MyIterator: public std::iterator<input_iterator_tag, MyClass> {
    // requirements for iterators
    MyIterator(const MyIterator&);
    MyIterator& operator=(const MyIterator&); // or any variant
    ~MyIterator() noexcept;
    MyClass& operator*() const;
    MyIterator& operator++();

    // requirements for input iterators
    MyClass* operator->();
    MyIterator operator++(int);
};

// requirements for iterators
bool operator==(const MyIterator& a, const MyIterator& b);
// requirements for input iterators
bool operator!=(const MyIterator& a, const MyIterator& b);

The implemented iterator should have a reference to the used map (or the MyMap object, depending on how do you implement it), to the vector in the map (for example, using an iterator) and to the element in the vector (again, for example, using an iterator). 实施的迭代器应参考使用的地图(或MyMap对象,具体取决于您如何实现),地图中的向量(例如,使用迭代器)和向量中的元素(同样,例如,使用迭代器)。

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

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