【无标题】

@[toc迭代器

示例

#include <iostream>
#include <vector>
// This program will create a vector of integers, add elements to it in a loop, and then iterate through the vector using an iterator.
int main() {
    // Create a vector of integers
    std::vector<int> inVect;
    // Loop through the vector, adding elements to it
    for (int i = 0; i < 10; i += 2) {
        inVect.push_back(i);
        // Print the element
        std::cout << i << std::endl;
        // Create an iterator
        std::vector<int>::iterator it = inVect.begin();
        // Loop through the vector, printing each element
        while (it != inVect.end()) {
            std::cout << *it++ << std::endl;   
        }
    }
}
int main() {
    std::vector<int> inVect(5);
    std::vector<int>::iterator out=inVect.begin();
    *out++ = 1;
    *out++ = 2;
    *out++ = 3;
    *out++ = 4;
    *out++ = 5;
    *out = 9;
    std::cout << "Vect:";
    std::vector<int>::iterator it = inVect.begin();
    while (it != inVect.end()) {
        std::cout << *it++ << " ";
    }
}

基本使用

#include <iostream>

#include <vector>

int main() {

    // 创建一个整数类型的vector

    std::vector<int> numbers = {10, 20, 30, 40, 50};

    // 使用迭代器遍历vector

    for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) {

        std::cout << *it << " "; // 使用解引用操作符(*)来访问迭代器指向的元素

    }

    std::cout << std::endl;

    // 使用范围基for循环遍历vector

    for (int num : numbers) {

        std::cout << num << " ";

    }

    std::cout << std::endl;

    // 使用迭代器修改vector中的元素

    for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) {

        *it += 5; // 给每个元素增加5

    }

    // 打印修改后的vector

    for (int num : numbers) {

        std::cout << num << " ";

    }

    std::cout << std::endl;

    // 使用迭代器查找特定元素

    std::vector<int>::iterator find_it = std::find(numbers.begin(), numbers.end(), 35);

    if (find_it != numbers.end()) {

        std::cout << "Found 35 at position: " << find_it - numbers.begin() << std::endl;

    }

    // 使用迭代器删除特定元素

    numbers.erase(find_it); // 删除找到的元素35

    for (int num : numbers) {

        std::cout << num << " ";

    }

    std::cout << std::endl;

    return 0;

}

高级使用


1. 反向迭代器: 反向迭代器允许从容器的末尾向前遍历。
for (auto it = numbers.rbegin(); it != numbers.rend(); ++it) {
    std::cout << *it << " ";
}
2. 插入元素: 使用迭代器和  insert  方法在特定位置插入元素。
numbers.insert(numbers.begin() + 2, 25); // 在索引2的位置插入25
3. 删除多个元素: 使用迭代器和  erase  方法删除一定范围内的元素。
auto to_erase = numbers.begin() + 1; // 指向要删除的第一个元素
numbers.erase(to_erase, numbers.begin() + 3); // 删除从索引1到索引2的元素
4. 使用  std::advance  函数:  std::advance  用于将迭代器向前或向后移动指定的步数。
std::vector<int>::iterator it = numbers.begin();
std::advance(it, 3); // 将迭代器it向前移动3个位置
5. 迭代器算术: 使用算术运算符来移动迭代器。
it += 2; // 将迭代器it向前移动2个位置
it -= 1; // 将迭代器it向后移动1个位置
6. 迭代器比较: 除了  ==!= ,迭代器还可以使用  < ,  > ,  <= ,  >=  进行比较。
if (it < numbers.end()) {
    // it是有效的,并且不是指向容器末尾的迭代器
}
7. 迭代器和算法: 迭代器经常与STL算法一起使用,如  std::sort ,  std::find_if ,  std::copy  等。
std::sort(numbers.begin(), numbers.end()); // 排序vector
auto even_it = std::find_if(numbers.begin(), numbers.end(), [](int x) { return x % 2 == 0; }); // 找到第一个偶数
8. 自定义迭代器: 对于自定义容器类,可以定义自己的迭代器来提供对容器元素的访问。
class MyContainer {
private:
    std::vector<int> data;
public:
    class Iterator {
    public:
        // 迭代器的实现细节
    };
    Iterator begin() { return Iterator(/* ... */); }
    Iterator end() { return Iterator(/* ... */); }
};
9. 迭代器失效和不变性: 当容器被修改时,一些迭代器可能会失效。了解不同容器的迭代器失效规则是很重要的。
10. 迭代器类型转换: 有时可能需要将一种类型的迭代器转换为另一种类型,例如,从  iterator  转换为  const_iterator 。
auto it = numbers.begin();
std::vector<int>::const_iterator cit = it;

进阶使用


1. 迭代器的类型转换: 使用  std::iterator_traits  可以获取迭代器的属性,例如它的值类型、指针类型等。
typedef std::iterator_traits<std::vector<int>::iterator> traits;
typedef traits::value_type ValueType; // 获取值类型,这里是int
2. 迭代器的通用表达式: C++标准库定义了一组通用算法,这些算法使用迭代器来操作容器,例如  std::copy ,  std::fill ,  std::remove_if  等。
std::vector<int> source = {1, 2, 3};
std::vector<int> destination;
std::copy(source.begin(), source.end(), std::back_inserter(destination));
3. 迭代器适配器: 迭代器适配器是包装了另一个迭代器的迭代器,提供了不同的行为或接口。例如, std::reverse_iterator  可以反向遍历容器。
std::vector<int> vec = {1, 2, 3, 4, 5};
for (std::reverse_iterator<std::vector<int>::iterator> rit(vec.rbegin()); 
     rit != vec.rend(); ++rit) {
    std::cout << *rit << " ";
}
4. 迭代器的兼容性: 迭代器的兼容性是C++模板编程中的一个重要概念。 std::iterator_traits  可以用于获取迭代器的兼容性信息。
5. 迭代器的自定义实现: 对于自定义容器,你可能需要实现自定义迭代器,以满足特定操作的需求。
class MyContainer {
private:
    std::vector<int> data;
public:
    class Iterator {
    public:
        // 自定义迭代器的实现
        int& operator*() { return data; } // 例如,返回引用
        // 其他必要的操作符重载
    };

    Iterator begin() { return Iterator(/* ... */); }
    Iterator end() { return Iterator(/* ... */); }
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值