S
Shailesh Humbad
I have a class and map like this:
class MyObject {
...
};
map<int, MyObject> Box;
Now, I want to reassign the keys of one or more of the pairs in Box
based on another map<int, int> that stores the key assignment like
(old, new). Since MyObject may be big, this should be done without
invoking the MyObject copy constructor. For example, if the map was
like (1 => a, 2 => b, 3 => c), then it might change to (3 => a, 1 =>
b, 2 => c). I figured out how to do the reassignment with STL, but
I'm not sure how to prevent the copying. Any suggestions?
map<int, MyObject> Box;
void
ReassignKeys(std::map<int, int>& ReassignmentMap)
{
map<int, MyObject> tempBox;
map<int, int>::iterator ReassignmentIterator;
map<int, MyObject>::iterator ObjectsIterator;
// Do nothing if nothing to reassign.
if(ReassignmentMap.size() == 0)
{
return;
}
// Loop through each reassignment and add the object to the new
for(ReassignmentIterator = ReassignmentMap.begin();
ReassignmentIterator != ReassignmentMap.end();
ReassignmentIterator++)
{
// First make sure the old key exists
assert(Box.end() !=
Box.find(ReassignmentIterator->first));
// Now get the object based on the old key, and add it to the
// temporary container with the new key
tempBox.insert(map<int, MyObject>::value_type(
ReassignmentIterator->second, // new key
Box[ReassignmentIterator->first]) ); // find object by old
// Remove the object from the original container
Box.erase(ReassignmentIterator->first);
}
// Add any objects that weren't reassigned to the new container
for(ObjectsIterator = Box.begin();
ObjectsIterator != Box.end(); ObjectsIterator++)
{
// Make sure the key doesn't already exist in the new map
assert(tempBox.end() == tempBox.find(ObjectsIterator->first));
// Insert the pair into the new map
tempBox.insert(map<int, MyObject>::value_type(
ObjectsIterator->first, ObjectsIterator->second));
}
// Erase the old map
Box.clear();
// Now swap the old with the new
Box.swap(tempBox);
}
class MyObject {
...
};
map<int, MyObject> Box;
Now, I want to reassign the keys of one or more of the pairs in Box
based on another map<int, int> that stores the key assignment like
(old, new). Since MyObject may be big, this should be done without
invoking the MyObject copy constructor. For example, if the map was
like (1 => a, 2 => b, 3 => c), then it might change to (3 => a, 1 =>
b, 2 => c). I figured out how to do the reassignment with STL, but
I'm not sure how to prevent the copying. Any suggestions?
map<int, MyObject> Box;
void
ReassignKeys(std::map<int, int>& ReassignmentMap)
{
map<int, MyObject> tempBox;
map<int, int>::iterator ReassignmentIterator;
map<int, MyObject>::iterator ObjectsIterator;
// Do nothing if nothing to reassign.
if(ReassignmentMap.size() == 0)
{
return;
}
// Loop through each reassignment and add the object to the new
for(ReassignmentIterator = ReassignmentMap.begin();
ReassignmentIterator != ReassignmentMap.end();
ReassignmentIterator++)
{
// First make sure the old key exists
assert(Box.end() !=
Box.find(ReassignmentIterator->first));
// Now get the object based on the old key, and add it to the
// temporary container with the new key
tempBox.insert(map<int, MyObject>::value_type(
ReassignmentIterator->second, // new key
Box[ReassignmentIterator->first]) ); // find object by old
// Remove the object from the original container
Box.erase(ReassignmentIterator->first);
}
// Add any objects that weren't reassigned to the new container
for(ObjectsIterator = Box.begin();
ObjectsIterator != Box.end(); ObjectsIterator++)
{
// Make sure the key doesn't already exist in the new map
assert(tempBox.end() == tempBox.find(ObjectsIterator->first));
// Insert the pair into the new map
tempBox.insert(map<int, MyObject>::value_type(
ObjectsIterator->first, ObjectsIterator->second));
}
// Erase the old map
Box.clear();
// Now swap the old with the new
Box.swap(tempBox);
}