Combining multiple maps with vectors into one map - c++

I have a question about combining maps that have vectors as the value section. for instance, I might have the following:
std::map<int, std::vector<Affector*> > affectors;
I want to build this map by combining multiple smaller maps. For instance:
for (auto ch = chList.begin(); ch != chList.end(); ++ch)
{
std::map<int, std::vector<Affector*> > tempAff = ch->getemng()->getAffectorsInOrder();
std::map<int, std::vector<Affector*> > tempAff2 = ch->getpmng()->getAffectorsInOrder()
//I want to append both of these maps to the top level affectors map
}
I can think of the obvious solution which would be
for (auto ch = chList.begin(); ch != chList.end(); ++ch)
{
std::map<int, std::vector<Affector*> > tempAff = ch->getemng()->getAffectorsInOrder();
for (auto aff = tempAff.begin(); aff != tempAff.end(); ++aff)
{
affectors[aff->first].push_back(aff->second);
}
tempAff.clear();
tempAff = ch->getpmng()->getAffectorsInOrder();
for (auto aff = tempAff.begin(); aff != tempAff.end(); ++aff)
{
affectors[aff->first].push_back(aff->second);
}
...
}
This will work, but feels inefficient. I can't use the insertion operation of the map since I need to preserve existing values in the vectors. Is there a better way to combine the maps I'm not thinking of?
Thanks

As mentioned by Richard Corden I think that you really want to be using a std::multimap.
std::multimap<int, Affector*> affectors;
If you also make tempAff and tempAff2 std::multimaps you can do:
affectors.insert(tempAff.begin(), tempAff.end());
affectors.insert(tempAff2.begin(), tempAff2.end());

Related

Retrieve first key of std::multimap, c++

I want to retrieve just the first key of a multimap. I already achieved it with iterating through the multimap, taking the first key and then do break. But there should be a better way, but I do not find it.
int store_key;
std::multimap<int, int> example_map; // then something in it..
for (auto key : example_map)
{
store_key = key;
break;
}
This solves the Problem, but I am searching for another solution.
Your range based for loop is more or less (not exactly but good enough for this answer) equivalent to:
for (auto it = example_map.begin(); it != example_map.end(); ++it) {
auto key = *it;
store_key = key;
break;
}
I hope now it is clear that you can get rid of the loop and for a non-empty map it is just:
auto store_key = *example_map.begin();
Note that store_key is a misnomer, because it is not just the key and your code would trigger a compiler error. It is a std::pair<const int,int>. store_key->first is the key.

How can I access the values of a map as a whole

I have a map in c++ like this:
std::map<int, int> points;
I know that I can access the two integers for example in a for loop like this
for (auto map_cntr = points.begin(); map_cntr != points.end(); ++map_cntr)
{
int A = map_cntr->first; // key
int B = map_cntr->second; // val
}
But I want to know how I can access every point as a whole (and not it's entries like above).
I thought something like this:
for (auto map_cntr = points.begin(); map_cntr != points.end(); ++map_cntr)
{
auto whole_point = points.at(map_cntr);
}
Actually, I want to do operations on integers of a entry (point) of the map with integers of the following entry (point) of the map.
I want to do operations on integers of a entry (point) of the map
with integers of the following entry (point) of the map.
Map is not suited container to perform operation depending on the sequence of elements where you want to modify current element according to previous ones. For those things you can use a vector or an array of pairs for instance.
You can use foreach loop
std::map<int, int> points;
for (auto pair : points)
{
// pair - is what you need
pair.second;
pair.first;
auto whole_point = pair;
}
I want to do operations on integers of a entry (point) of the map with integers of the following entry (point) of the map
You can't directly modify the key of a [key,value] pair in a map. If you need to do so, you have to erase the pair and insert another one.
If you only need to write the value of a pair, or if you only need to read the pairs, you can do it with a single iterator, like this:
// assuming the map contains at least 1 element.
auto it = points.begin();
std::pair<const int, int>* currentPoint = &(*it);
it++;
for (; it != points.end(); ++it) {
auto& nextPoint = *it;
// Read-only: currentPoint->first, nextPoint.first
// Read/write: currentPoint->second, nextPoint.second
currentPoint = &nextPoint;
}
Live example

read set from map in c++

It's very silly question but somehow I am not able to solve.
I have map who key is string and value is set and I want to iterate through value and print it for example
std::map<std::string, std::set<std::string>> test_map_set
for (auto it_map = test_map_set["test"].begin(); it_mpa != test_map_set["test"].begin(); it_map ++ )
{
auto it = it_map->second; ===> Here I am getting error that it has no member second
then iterate through set
}
My question is how to iterate through set ?
Indeed std::string has no member second.
Since you have c++11, you can make life a lot easier:
std::map<std::string, std::set<std::string>> test_map_set
for (std::string& set_element : test_map_set["test"])
{
}
For deleting:
auto& test = test_map_set["test"];
for (auto it = test.begin(); it!= test.end();)
{
if (it->length()==5)
it = test.erase(it);
else
++it;
}
This would delete all strings of 5 characters

boost unordered_multimap loop over the unique keys

What is the easiest way to loop over the unique keys in a boost unordered_multimap.
For example I have this:
std::set<int> used;
for (auto p : valuesMap)
{
if (used.count(p.first))
continue;
used.insert(p.first);
auto range = valuesMap.equal_range(p.first);
if (p.first)
for (auto v = range.first; v != range.second; ++v)
//do something;
}
Is there better way to do that. It seems like the unique keys should be already known to the unordered map.
What you want to do is find a way to get the iterator following a certain key. In multimap I'd usually use upper_bound. But since unordered_multimap doesn't have that - I'll have to use equal_range.second:
for (auto iter=valueMap.begin();
iter!=valueMap.end();
iter=ValueMap.equal_range(iter->first)->second){
uniq_key=iter->first;
// Do whatever you want with uniq_key
}
But your example is weird to me - because you DO go over all the element . If I wanted to write your code, doing what you do, this is how I'd do it:
for (auto iter=valueMap.begin()
iter!=valueMap.end();
){ // Notice the lack of ++iter!!!
auto end=valueMap.equal_range(ier->first)->second;
for (;iter!=end;++iter)
// Do something
}

std::set_difference is it possible to compare set and map Keys?

So we get a new set of strings, and we have one as map Keys. And we want to do one way set_difference (note - not set_symmetric_difference). So currently I have such ugly code like:
std::map<std::string, boost::shared_ptr<some_class> > _ds;
std::set<std::string> compare_and_clean(std::set<std::string> &new_)
{
std::set<std::string> result;
std::set<std::string> old;
for (std::map<std::string, std::string>::iterator mi = _ds.begin(); mi != _ds.end(); ++mi)
old.insert(mi->first);
std::set_difference( old.begin(), old.end(), new_.begin(), new_.end(), inserter(result, result.begin()));
for (std::set<std::string>::iterator i = result.begin(); i != result.end(); ++i) {
_ds.erase(*i);
}
return result;
}
I wonder how to do set_difference over map Keys and set in more clean way?
Yes: You can iterate over just the keys of the std::map using a transform iterator.
You can find two implementations of such a transform iterator (one using Boost, the other standalone) in an answer I provided to another question.