I have two maps, tk1 and tk2 with the following structure:
std::map<std::string, double>tk1;
std::map<std::string, double>tk2;
tk1 contains the following data:
2011-01-03 2200
2011-01-04 2209
2011-01-05 2300
and tk2 contains the following data:
2011-01-03 2450
2011-01-04 2465
2011-01-06 2476
2011-01-07 2457
I have created a set that contains the dates as strings as in
std::set<std::string>dateset;
std::set<std::string>new_dateset;
I created it by iterating through the 2 maps and inserting into the set as in
dateset.insert(it->first);
dateset has the following values:
2011-01-03
2011-01-04
2011-01-05
2011-01-06
2011-01-07
I want to populate new_dateset so it only contains the dates that are in both tk1 and tk2, i.e. new_dateset should contain only
2011-01-03
2011-01-04
I wrote the following:
std::set<std::string>::iterator it1=dateset.begin(), end1=dateset.end();
std::map<std::string, double>::iterator it2=tk1.begin(), end2=tk1.end();
std::map<std::string, double>::iterator it3=tk2.begin(), end3=tk2.end();
while (it1 != end1) {
if (it2->first == *it1)
new_dateset.insert(it2->first);
++it1;
}
but obviously I am not doing it correctly. Can someone suggest the best way to do this.
Iterate over
it1, check iffirstfor each element is intk2using thefind()method, and insert it into the set if it is there. What you are doing in your example is to check elements indataset, which contain keys that are intk1ORtk2.