C++ 迭代器如何映射/知道其当前位置或元素

C++ 迭代器如何映射/知道其当前位置或元素,c++,for-loop,iterator,C++,For Loop,Iterator,考虑以下代码示例: #include <vector> #include <numeric> #include <algorithm> #include <iterator> #include <iostream> #include <functional> int main() { std::vector<int> v(10, 2); std::partial_sum(v.cbegin(),

考虑以下代码示例:

#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <functional>

int main()
{
    std::vector<int> v(10, 2);
    std::partial_sum(v.cbegin(), v.cend(), v.begin());
    std::cout << "Among the numbers: ";
    std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';

    if (std::all_of(v.cbegin(), v.cend(), [](int i){ return i % 2 == 0; })) {
        std::cout << "All numbers are even\n";
    }
    if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<int>(), 
                                                     std::placeholders::_1, 2))) {
        std::cout << "None of them are odd\n";
    }
    struct DivisibleBy
    {
        const int d;
        DivisibleBy(int n) : d(n) {}
        bool operator()(int n) const { return n % d == 0; }
    };

    if (std::any_of(v.cbegin(), v.cend(), DivisibleBy(7))) {
        std::cout << "At least one number is divisible by 7\n";
    }
}

在这个for循环示例中,很明显我们正在处理的当前元素是
i
,因为我们实际上正在访问
v[i]
。但为什么在相同代码的迭代器版本中,它映射了
i
,或者知道我们正在访问的当前元素是什么

[](int i){return i%2==0;})
如何确保/知道i是迭代器指向的当前元素


如果不使用任何
v,我无法理解。目前,我处于这个位置()
,迭代是如何完成的。我知道迭代器是什么,但我很难掌握它们。谢谢:)

迭代是通过使用

迭代器是任何对象,它指向 元素(如数组或容器)具有迭代的能力 使用一组运算符(至少 最少使用增量(++)和取消引用(*)运算符)

迭代器最明显的形式是指针:指针可以指向 元素,并可以使用增量对其进行迭代 运算符(++)

并通过一组元素推进它。代码中的
std::all_
函数大致相当于以下代码

template< class InputIt, class UnaryPredicate >
bool c_all_of(InputIt first, InputIt last, UnaryPredicate p)
{
    for (; first != last; ++first) {
        if (!p(*first)) {
            return false; // Found an odd element!
        }
    }
    return true; // All elements are even
}
template
bool c_all_of(先输入,后输入,一元谓词p)
{
for(;first!=last;++first){
如果(!p(*第一个)){
return false;//发现一个奇数元素!
}
}
return true;//所有元素都是偶数
}
迭代器在递增时跟踪当前指向的元素,在取消引用时返回当前指向的元素的值

为了教学和清晰起见,您还可以考虑以下操作(不要在家里尝试)

bool c_all_of(int*firstElement,size\t numberOfElements,std::function evenTest)
{
对于(大小i=0;i

请注意,迭代器是一种功能强大的抽象,因为它们允许在不同的容器中访问一致的元素(例如,
std::map
)。

迭代器是根据指针建模的,事实上就是这样。它们如何在内部工作并不重要,但一个可能的实现是在内部实际有一个指向当前元素的指针。

执行
[](int i){return i%2==0;})
内部
std::any_of
,它如何知道lambda中使用的i实际上是当前迭代器的值?这是lamda函数的某些属性吗?@AbhinavGauniyal i不是向量的索引,它是向量的元素,即被解引用的迭代器或指向被解引用元素的指针。这在很大程度上消除了我的困惑。最后一个问题是,
i
何时实际传输了当前指针/迭代器的值?当它在lambda被调用之前被传递到lamda?@AbhinavGauniyal时,指向正确元素的指针/迭代器/任何东西都被取消引用。lambda会自动获取正确的值作为其参数。取消引用迭代器/指针/任何写在标准库头中的代码(知道哪一行和哪一列这样做并不重要)。
for(int i = 0; i<v.size();++i){
    if(v[i] % 2 == 0) areEven = true;    //just for readablity
    else areEven = false;
}
template< class InputIt, class UnaryPredicate >
bool c_all_of(InputIt first, InputIt last, UnaryPredicate p)
{
    for (; first != last; ++first) {
        if (!p(*first)) {
            return false; // Found an odd element!
        }
    }
    return true; // All elements are even
}
bool c_all_of(int* firstElement, size_t numberOfElements, std::function<bool(int)> evenTest)
{
    for (size_t i = 0; i < numberOfElements; ++i)
        if (!evenTest(*(firstElement + i)))
            return false;
    return true;
}