如何在基于范围的for循环中找到当前对象的索引?

编程入门 行业动态 更新时间:2024-10-10 07:25:18
本文介绍了如何在基于范围的for循环中找到当前对象的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有以下代码:

vector<int> list; for(auto& elem:list) { int i = elem; }

我可以在不维护单独迭代器的情况下找到 elem 在向量中的位置吗?

Can I find the position of elem in the vector without maintaining a separate iterator?

推荐答案

是的,只是需要一些按摩;)

Yes you can, it just take some massaging ;)

诀窍是使用组合:您无需直接遍历容器,而是在整个过程中使用索引压缩"它.

The trick is to use composition: instead of iterating over the container directly, you "zip" it with an index along the way.

专业的邮政编码:

template <typename T> struct iterator_extractor { typedef typename T::iterator type; }; template <typename T> struct iterator_extractor<T const> { typedef typename T::const_iterator type; }; template <typename T> class Indexer { public: class iterator { typedef typename iterator_extractor<T>::type inner_iterator; typedef typename std::iterator_traits<inner_iterator>::reference inner_reference; public: typedef std::pair<size_t, inner_reference> reference; iterator(inner_iterator it): _pos(0), _it(it) {} reference operator*() const { return reference(_pos, *_it); } iterator& operator++() { ++_pos; ++_it; return *this; } iterator operator++(int) { iterator tmp(*this); ++*this; return tmp; } bool operator==(iterator const& it) const { return _it == it._it; } bool operator!=(iterator const& it) const { return !(*this == it); } private: size_t _pos; inner_iterator _it; }; Indexer(T& t): _container(t) {} iterator begin() const { return iterator(_container.begin()); } iterator end() const { return iterator(_container.end()); } private: T& _container; }; // class Indexer template <typename T> Indexer<T> index(T& t) { return Indexer<T>(t); }

并使用它:

#include <iostream> #include <iterator> #include <limits> #include <vector> // Zipper code here int main() { std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9}; for (auto p: index(v)) { std::cout << p.first << ": " << p.second << "\n"; } }

您可以在 ideone 上看到它,尽管它缺少for-range循环支持,因此不太美观.

You can see it at ideone, though it lacks the for-range loop support so it's less pretty.

只记得我应该更频繁地检查Boost.Range.不幸的是,没有 zip 范围,但是我确实找到了一个perl: boost :: adaptors :: indexed .但是,它需要访问迭代器才能提取索引.丢人:x

Just remembered that I should check Boost.Range more often. Unfortunately no zip range, but I did found a perl: boost::adaptors::indexed. However it requires access to the iterator to pull of the index. Shame :x

否则,使用 counting_range 和通用的 zip 我相信可以做一些有趣的事情...

Otherwise with the counting_range and a generic zip I am sure it could be possible to do something interesting...

在理想世界中,我会想象:

In the ideal world I would imagine:

int main() { std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9}; for (auto tuple: zip(iota(0), v)) { std::cout << tuple.at<0>() << ": " << tuple.at<1>() << "\n"; } }

使用 zip 自动创建一个视图,作为引用元组的范围,而 iota(0)只需创建一个从 0 ,并且仅计入无穷大(或者,它的最大值……).

With zip automatically creating a view as a range of tuples of references and iota(0) simply creating a "false" range that starts from 0 and just counts toward infinity (or well, the maximum of its type...).

更多推荐

如何在基于范围的for循环中找到当前对象的索引?

本文发布于:2023-11-08 20:49:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   对象   中找到   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!