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

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.

Related

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

Combining multiple maps with vectors into one map

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

C++ Intersect two maps on keys, keep value of first map

I'm experiencing a problem with C++ and maps and intersection.
Have 3 maps, the first two being map<int, double>, and the last one being map<int, CustomType>.
I want to remove all instances of map keys from the first 2 maps, that do not exist as a key in the 3rd map. In brief, I have the third map that contains a list of objects, and the first two maps that contain some data about the objects. At some point in time the map with the objects is cleaned up and some items removed (user interaction) and now want to clean up the other two maps respectively.
I've tried the following:
map<int, double> map1, map2;
map<int, CustomType> map3;
for (auto it = map1.cbegin(); it != map1.cend(); )
{
if ( map3.find(it->first) == map3.end() )
{
map2.erase(it);
map1.erase(it++);
}
else ++it;
}
This gives me an error "pointer being freed was not allocated" on the map1.erase line. I've looked into set_intersection but I don't believe it would work in this case since the values will be different.
Any assistance is appreciated.
You need to iterate map1 and map2 independently. You cannot use an iterator of map1 to manipulate another map (to erase from map2 or to perform any other operations with map2).
So the code should be something like this:
map<int, double> map1, map2;
map<int, CustomType> map3;
for (auto it = map1.cbegin(); it != map1.cend(); )
{
if ( map3.find(it->first) == map3.end() )
it = map1.erase(it);
else
++it;
}
for (auto it = map2.cbegin(); it != map2.cend(); )
{
if ( map3.find(it->first) == map3.end() )
it = map2.erase(it);
else
++it;
}
You are trying to erase an element from map2 with an iterator from map1. That won't work. You need to get the key value from the iterator and use that to erase from map2. And when you called erase for map1 you invalidated your iterator, because you removed the element it was pointing to. Increment the iterator, then use the key value to call map1.erase().
You was close to the solution, but the problem is that an iterator is substantially a pointer.
So you can use "it" to remove both on map1 and map2.
void removeUnexist(const map<int, double>& m, const map<int, CustomType>::iterator& it) {
i = m.find(it->first);
if(i == m.end()) {
m.erase(i);
}
}
map<int, double> map1, map2;
map<int, CustomType> map3;
for (auto it = map3.cbegin(); it != map3.cend(); it++) {
removeUnexist(map1, it);
removeUnexist(map2, it);
}

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
}

STL Multimap Remove/Erase Values

I have STL Multimap, I want to remove entries from the map which has specific value , I do not want to remove entire key, as that key may be mapping to other values which are required.
any help please.
If I understand correctly these values can appear under any key. If that is the case you'll have to iterate over your multimap and erase specific values.
typedef std::multimap<std::string, int> Multimap;
Multimap data;
for (Multimap::iterator iter = data.begin(); iter != data.end();)
{
// you have to do this because iterators are invalidated
Multimap::iterator erase_iter = iter++;
// removes all even values
if (erase_iter->second % 2 == 0)
data.erase(erase_iter);
}
Since C++11, std::multimap::erase returns an iterator following the last removed element.
So you can rewrite Nikola's answer slightly more cleanly without needing to introduce the local erase_iter variable:
typedef std::multimap<std::string, int> Multimap;
Multimap data;
for (Multimap::iterator iter = data.begin(); iter != data.end();)
{
// removes all even values
if (iter->second % 2 == 0)
iter = data.erase(iter);
else
++iter;
}
(See also answer to this question)