c++ - 在 map 中查找集合的元素

标签 c++

我有两个 map ,tk1tk2,结构如下:

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,使其只包含 tk1tk2 中的日期,即 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;
}

但显然我没有做对。有人可以建议最好的方法吗。

最佳答案

您可能会考虑使用来自 previous answerkey_iteratorstd::set_intersection .

关于c++ - 在 map 中查找集合的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10565723/

相关文章:

c++ - 以编程方式设置应用程序的播放设备

c++ - 如何静态断言枚举元素的值?

c++ - 局部作用域和函数作用域的区别

c++ - 什么是 FFI 扩展?

c++ - 排队的事件数量? (Win32 事件队列)

c++ - 打印您使用参数定义的方法名称

python - 计算 SHA-1 摘要时 Python 和 C++ 中的不同结果

c++ - CMake 包含和源路径与 Windows 目录路径不同

c++ - 如何让 eclipse CDT 忽略错误?

c++ - C++中Concurrent Queue + map的实现