在C ++ 11中找到元素的位置基于范围的for循环? [英] Find position of element in C++11 range-based for loop?
问题描述
假设我有以下代码:
vector< int&列表;
for(auto& elem:list){
int i = elem;
}
我可以找到 elem
是的,只需要一些按摩; / p>
诀窍是使用composition:而不是直接在容器上迭代,你可以使用索引来压缩它。
专用拉链代码:
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>参考;
迭代器(inner_iterator it):_pos(0),_it(it){}
引用运算符*()const {return reference(_pos,* _it); }
迭代器& 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;
};
索引器(T& t):_container(t){}
迭代器begin()const {return iterator(_container.begin()); }
iterator end()const {return iterator(_container.end()); }
private:
T& _容器;
}; // class Indexer
template< typename T>
Indexer< T>索引(T& t){return Indexer T(t); }
并使用它:
#include< iostream>
#include< iterator>
#include< limits>
#include< vector>
//这里的拉链代码
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<< \\\
;
}
}
您可以在 ideone ,但它缺少范围循环支持,因此不太漂亮。
编辑:
记住我应该经常检查Boost.Range。很遗憾,没有 zip
范围,但我发现了一个perl: boost :: adapters :: indexed
。但是它需要访问迭代器来拉取索引。耻辱:x
否则使用 counting_range
和一个通用的 zip
可以做一些有趣的事情...
在理想的世界里,我会想象:
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()<< \\\
;
}
}
使用 zip
自动创建一个作为引用元组范围的视图, iota(0)
只需创建一个从 0开始的
,并且只计入无穷大(或者好的,其类型的最大值...)。
Assume I have the following code:
vector<int> list;
for(auto& elem:list) {
int i = 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.
Specialized zipper code:
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); }
And using it:
#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";
}
}
You can see it at ideone, though it lacks the for-range loop support so it's less pretty.
EDIT:
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
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";
}
}
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...).
这篇关于在C ++ 11中找到元素的位置基于范围的for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!