小编orm*_*rin的帖子

c ++通过具有可变方向的向量迭代

我想知道是否有任何想法如何在输入迭代方向时迭代向量(或容器)的元素.

这是我能想到的第一件事:

    std::vector<int> vec = {1, 2, 3, 4};
    int direction = 1 // or -1;
    int start = direction == 1 ?  0 : (int)arrs.size()-1;
    for (int i=start; i<(int)vec.size() && 0<=i; i+=direction) {
      // do your stuff
    }
Run Code Online (Sandbox Code Playgroud)

有谁知道更好或更好的方法来做到这一点?

c++ stl

1
推荐指数
1
解决办法
182
查看次数

标签 统计

c++ ×1

stl ×1