How to update such map structure?
I have a std::map<boost::shared_ptr<some_class>, class_descripti开发者_JAVA技巧on> class_map;
where class_description
is:
//Each service provides us with rules
struct class_description
{
//A service must have
std::string name;
// lots of other stuff...
};
and I have another std::map<boost::shared_ptr<some_class>, class_description> class_map_new;
I need to insert pairs <boost::shared_ptr<some_class>, class_description>
from class_map_new
to class_map
in case there was no class_description
with such name
in class_map
before. How to do such thing?
std::map::insert
doesn't allow duplicates, so you can simply attempt to insert the new values:
//Each service provides us with rules
struct class_description
{
//A service must have
std::string name;
// lots of other stuff...
};
std::map<boost::shared_ptr<some_class>, class_description> class_map;
std::map<boost::shared_ptr<some_class>, class_description> class_map_new;
// insert the new values into the class_map
// using C++0x for simplicity...
for(auto new_obj = class_map_new.cbegin(), end = class_map_new.cend();
new_obj != end; ++new_obj)
{
auto ins_result = class_map.insert(*new_obj);
if(false == ins_result.second)
{
// object was already present,
// ins_result.first holds the iterator
// to the current object
}
else
{
// object was successfully inserted
}
}
What you want is an "stl like" algorithm for copy_if. There's not one, but you can probably find an example on the web, or write your own by looking at the code for count_if and remove_copy_if.
精彩评论