【发布时间】:2012-05-20 21:33:15
【问题描述】:
我有两张地图,tk1 和 tk2,结构如下:
std::map<std::string, double>tk1;
std::map<std::string, double>tk2;
tk1 包含以下数据:
2011-01-03 2200
2011-01-04 2209
2011-01-05 2300
而tk2 包含以下数据:
2011-01-03 2450
2011-01-04 2465
2011-01-06 2476
2011-01-07 2457
我创建了一个集合,其中包含日期作为字符串,如
std::set<std::string>dateset;
std::set<std::string>new_dateset;
我通过遍历 2 个地图并插入到集合中来创建它
dateset.insert(it->first);
dateset 具有以下值:
2011-01-03
2011-01-04
2011-01-05
2011-01-06
2011-01-07
我想填充 new_dateset,使其仅包含 tk1 和 tk2 中的日期,即 new_dateset 应仅包含
2011-01-03
2011-01-04
我写了以下内容:
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;
}
但显然我做得不对。有人可以建议最好的方法吗?
【问题讨论】:
标签: c++