iterator adapter to iterate just the values in a map?(迭代器适配器仅迭代地图中的值?)
问题描述
在做了几年 C# 和最近的 Objective C 之后,我才刚刚回到 C++.
I'm just getting back into C++ after a couple of years of doing a lot of C#, and recently Objective C.
我之前做过的一件事是为 std::map 滚动我自己的迭代器适配器,它将只引用值部分,而不是键值对.这是很常见和很自然的事情.C# 为这个工具提供了它的 Dictionary 类的 Keys 和 Values 属性.Objective-C 的 NSDictionary 同样具有 allKeys 和 allValues.
One thing I've done before is to roll my own iterator adapter for std::map that will deref to just the value part, rather than the key-value pair. This is quite a common and natural thing to do. C# provides this facility with its Keys and Values properties of its Dictionary class. Objective-C's NSDictionary, similarly, has allKeys and allValues.
自从我离开"以来,Boost 已经获得了 Range 和 ForEach 库,我现在正在广泛使用它们.我想知道两者之间是否有一些设施可以做同样的事情,但我找不到任何东西.
Since I've been "away", Boost has acquired the Range and ForEach libraries, which I am now using extensively. I wondered if between the two there was some facility to do the same, but I haven't been able to find anything.
我正在考虑使用 Boost 的迭代器适配器来解决问题,但在我走这条路之前,我想我想在这里问一下是否有人知道 Boost 中的这种设施,或者其他现成的地方?
I'm thinking of knocking something up using Boost's iterator adapters, but before I go down that route I thought I'd ask here if anyone knows of such a facility in Boost, or somewhere else ready made?
推荐答案
我不认为有什么开箱即用的.你可以使用 boost::make_transform.
I don't think there's anything out of the box. You can use boost::make_transform.
template<typename T1, typename T2> T2& take_second(const std::pair<T1, T2> &a_pair)
{
return a_pair.second;
}
void run_map_value()
{
map<int,string> a_map;
a_map[0] = "zero";
a_map[1] = "one";
a_map[2] = "two";
copy( boost::make_transform_iterator(a_map.begin(), take_second<int, string>),
boost::make_transform_iterator(a_map.end(), take_second<int, string>),
ostream_iterator<string>(cout, "
")
);
}
这篇关于迭代器适配器仅迭代地图中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:迭代器适配器仅迭代地图中的值?
基础教程推荐
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01