samedi 24 janvier 2015

Map containing map iterators

Consider the following class:



#include <string>
#include <vector>
#include <map>

class a
{
private:
struct map_vector;

typedef std::map <std::string, map_vector>::iterator map_iterator;

struct map_vector
{
std::vector <map_iterator> my_vec;
};
private:
std::string my_string;
std::map <std::string, map_vector> my_map;
public:
a() : my_string("\0")
{
my_map.insert(std::pair <std::string, map_vector>(my_string, map_vector()));
};

void addToken(const std::string &token)
{
// add current token if it does not exist
map_iterator a_it1 = my_map.find(token);
if(a_it1 == my_map.end())
{
std::pair <map_iterator, bool> res = my_map.insert(std::pair <std::string, map_vector>(token, map_vector()));
a_it1 = res.first;
}

// add iterator of current token to previous token
map_iterator a_it2 = my_map.find(my_string);
a_it2->second.my_vec.push_back(a_it1);

// set previous token to current token
my_string = token;
}
};


Is there a way to remove the map_vector subobject entirely or rework it? I'm not sure how I could do this because I need a map of <string, map iterator> where the map iterator refers to the original map itself.


For example it would be nice if in addToken I could do the following directly:



a_it2->second.push_back(a_it1);


I looked at the following links:



Aucun commentaire:

Enregistrer un commentaire