在嵌套的基于范围的for循环

时间:2015-10-25 18:38:01

标签: c++ c++11 vector iterator auto

目前我已开始使用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

   }    
}

我完全不知道怎么做。请建议适当的方法。提前谢谢。

2 个答案:

答案 0 :(得分:3)

我假设您要使用auto 新的基于范围的for循环。

您可以创建vector_view并迭代&#34;子矢量&#34;在内循环中。

这是一个简单的示例,可以帮助您入门(请注意使用auto&而不是auto):

Run It Online

#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_};
}

感谢模板参数类型推导,你可以写:

Run It Online

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;
}

答案 1 :(得分:2)

auto it:v

是......的简写。

auto it = v.begin(); it != v.end(); it++

所以在嵌套for循环中使用 auto ,更长的版本更合适......

#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;
}