I'm having problems with unique().
I have some code that goes something like this:
std::map<int, CompCont> sysRelMap;
for each(pair<int, CompCont> comp in sysRelMap)
{
list<pair<string,string>>::iterator newEnd =
unique(comp.second.connList.begin(), comp.second.connList.end(), equalPair);
comp.second.connList.erase(newEnd,comp.second.connList.end());
}
equalPair is a small, simple function:
bool ScriptWriter::equalPair(const pair<string, string> first, const pair<string, string> second)
{
return (first.first.compare(second.first) == 0) && (first.second.compare(second.second) == 0);
}
CompCont is a class I created that contains, for this example, one important data member, a list< pair<string,string> > connList. When I set a breakpoint at the end of each for loop, it lists comp.second.connList correctly (with only unique pairs) but as soon as it breaks out of that for loop sysRelMap goes back to having the values of each CompCont.connList to what it was before unique() and erase() were called. Hopefully this is understandable.
I'm assuming it's somehow that my functions are being passed copies of the list objects instead of references to them, but am not sure how I would modify my code to handle. I just followed many of the examples online for how to do this with the Binary Predicate function, and they all looked similar to what I have. I didn't see anything about passing references, etc, but they also weren't dealing with lists encapsulated in other objects.
I thought this might work:
for each(pair<int, CompCont> comp in sysRelMap)
{
list<pair<string,string>> modList = comp.second.connList;
list<pair<string,string>>::iterator newEnd =
unique(modList.begin(), modList.end(), equalPair);
modList.erase(newEnd,modList.end());
comp.second.connList = modList;
}
.. but it didn't. Still, afterwards, the list objects in each CompCont in the sysRelMap remain the same as they were before the for each loop.
Any input would be greatly appreciated. Feel like I'm missing something small, and that's bugging me.