在基于嵌套范围的 for 循环中使用 auto(用于迭代)
Using auto (for iterating) in nested range-based for loop
目前我已经开始使用auto
关键字。我对此有些怀疑:
如果我需要遍历vector
我会这样做:
vector<int>v;
for(auto it : v){
cout << it <<endl;
}
但是假设我需要做这样的事情:
vector<int>v;
for(auto it:v){
for(auto jt:X){
//where X is next position of it's position
//What I mean is if it is currently at 2nd position then
//jt iterator will start from 3rd position
}
}
我完全不知道该怎么做。请建议什么是合适的方法。提前谢谢。
我假设你想使用auto
和新的基于范围的for
循环。
您可以创建一个vector_view
并迭代内部循环中的"子向量"。
这里有一个简单的例子来帮助你入门(注意使用auto&
而不是auto
(:
在线运行
#include <iostream>
#include <cstddef>
#include <numeric>
#include <vector>
using std::cout;
using std::endl;
template <typename T, typename A>
struct vector_view
{
using vector_type = std::vector<T, A>;
using const_iterator = typename vector_type::const_iterator;
using iterator = typename vector_type::iterator;
vector_type& vec;
size_t _begin;
size_t _length;
vector_view(vector_type& v, size_t begin_, size_t length_)
: vec(v), _begin(begin_), _length(length_) {}
const_iterator begin() const { return vec.begin() + _begin; }
iterator begin() { return vec.begin() + _begin; }
const_iterator end() const { return vec.begin() + _begin + _length; }
iterator end() { return vec.begin() + _begin + _length; }
};
int main()
{
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 0);
for (auto& it : v)
{
size_t begin = std::distance(&v[0], &it) + 1;
size_t length = v.size() - begin;
vector_view<typename decltype(v)::value_type,
typename decltype(v)::allocator_type
> vv(v, begin, length);
cout << it << ": ";
for (auto& jt : vv)
{
cout << jt << " ";
}
cout << endl;
}
}
输出:
0: 1 2 3 4 5 6 7 8 9
1: 2 3 4 5 6 7 8 9
2: 3 4 5 6 7 8 9
3: 4 5 6 7 8 9
4: 5 6 7 8 9
5: 6 7 8 9
6: 7 8 9
7: 8 9
8: 9
9:
编辑:如果您定义make_vector_view()
函数,则可以使语法不那么冗长:
template <typename T, typename A>
vector_view<T, A> make_vector_view(std::vector<T, A>& v,
size_t begin_,
size_t length_)
{
return {v, begin_, length_};
}
并且由于模板参数类型推导,您可以编写:
在线运行
for (auto& it : v)
{
size_t begin = std::distance(&v[0], &it) + 1;
size_t length = v.size() - begin;
cout << it << ": ";
for (auto& jt : make_vector_view(v, begin, length))
{
cout << jt << " ";
}
cout << endl;
}
auto it:v
是...
auto it = v.begin(); it != v.end(); it++
所以要在嵌套的循环中使用自动,更长的版本更合适......
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v(10, 17);
for (auto& it = v.begin(); it != v.end(); ++it) {
for (auto& it2 = it + 1; it2 != v.end(); ++it2) {
cout << *it2 << " ";
}
cout << endl;
}
// system("pause");
return 0;
}
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 迭代时从向量和内存中删除对象
- 如何在c++迭代器类型中包装std::chrono
- 带过滤器的现代迭代c++集合
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- C++矢量迭代
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 擦除while循环中迭代的元素
- 实现一个在集合上迭代的模板函数
- 使用"auto"进行迭代时,"&"符号有什么作用
- 将 const auto & 转换为迭代器
- 使用auto-ref或迭代器进行迭代
- 从模板中的迭代器推断变量的类型,out使用auto
- 具有代理迭代器/引用和auto的容器
- 对迭代器使用 auto
- 在基于嵌套范围的 for 循环中使用 auto(用于迭代)
- 将迭代器的左侧从auto替换为pair