iteratorc++用法详解

一、iterator用法

在 C++ 中,迭代器 (Iterator) 是一个非常重要的概念,在 STL 中被广泛应用。作为 C++ 的核心组成部分,迭代器是一种抽象的数据类型,被用来指向容器中的元素,并能够遍历这些元素。

以下是使用 vector 容器的迭代器遍历元素的示例代码:

#include 
#include 

int main() {
  std::vector vec{1, 2, 3, 4, 5};
 
  // 迭代器遍历 vector 容器
  for (auto it = vec.begin(); it != vec.end(); ++it) {
     std::cout << *it << " ";
  }
  return 0;
}

在这个示例中,迭代器通过 begin() 和 end() 实现对 vector 容器的遍历操作。其中,begin() 函数返回容器的第一个元素的迭代器,而 end() 函数则返回后一个元素的迭代器。在遍历时,使用迭代器指向的元素需要使用 * 符号进行访问。

二、iteratorc用法

除了迭代器的基本操作,还有一些高级用法被称为 iteratorc(Iterator Categories),主要分为输入迭代器、输出迭代器、前向迭代器、双向迭代器和随机访问迭代器五种。这五种迭代器类型在 STL 中被广泛应用,并且其中的任何一种操作,都可以通过具有指定抽象级别的操作实现。

下面的示例对五种迭代器类型进行了详细解释:

#include 
#include 
#include 
#include 

int main() {
  std::vector vec{1, 2, 3, 4, 5};
  std::list lst;

  // 输入迭代器示例
  std::copy(vec.begin(), vec.end(), std::back_inserter(lst));
  for (auto it = lst.begin(); it != lst.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;

  // 输出迭代器示例
  std::ostream_iterator out_it(std::cout, " ");
  std::copy(vec.begin(), vec.end(), out_it);
  std::cout << std::endl;

  // 前向迭代器示例
  std::forward_list flst{1, 2, 3, 4, 5};
  auto it = flst.before_begin();
  for (auto end = flst.end(); it != end; ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;

  // 双向迭代器示例
  std::list l{1, 2, 3, 4, 5};
  auto dl_it = l.begin();
  std::advance(dl_it, 3);
  l.insert(dl_it, -1);
  for (auto it = l.begin(); it != l.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;

  // 随机访问迭代器示例
  std::vector v{1, 2, 3, 4, 5};
  auto r_it = v.begin() + 2;
  std::cout << *r_it << " ";
  std::cout << r_it[1] << std::endl;

  return 0;
}

三、itisbefore用法

itisbefore (is iterator before) 是一个常用的迭代器比较函数,用于比较迭代器的大小。

下面是一个示例代码:

#include 
#include 
#include 

int main() {
  std::vector vec{3, 7, 2, 5, 1, 9};
  auto min_it = std::min_element(vec.begin(), vec.end());
  auto max_it = std::max_element(vec.begin(), vec.end());

  if (std::is_sorted(vec.begin(), vec.end())) {
    std::cout << "vec is sorted." << std::endl;
  } else {
    auto b_it = std::is_sorted_until(vec.begin(), vec.end());
    std::cout << "vec is not sorted, first unsorted element is: " << *b_it << std::endl;
  }

  if (std::is_sorted(vec.begin(), min_it, std::less())) {
    std::cout << "First part of vec is sorted." << std::endl;
  } else {
    std::cout << "First part of vec is not sorted." << std::endl;
  }

  if (std::is_sorted(max_it, vec.end(), std::greater())) {
    std::cout << "Second part of vec is sorted." << std::endl;
  } else {
    std::cout << "Second part of vec is not sorted." << std::endl;
  }

  return 0;
}

在这个示例中,使用了 is_sorted 和 is_sorted_until 函数判断了 vec 是否已经排序。此外,使用了 std::less 和 std::greater 比较函数分别对前半部分和后半部分进行了排序比较。

四、iterator接口的用法

在 STL 中,迭代器实现了一些通用的接口,包括大小、增减、比较等等。这些接口使得使用迭代器时变得更为灵活和方便。

以下是一些基础的迭代器接口示例代码:

#include 
#include 

int main() {
  std::vector vec{1, 2, 3, 4, 5};

  std::cout << "First element of vec: " << *vec.begin() << std::endl;
  std::cout << "Last element of vec: " << *(vec.end() - 1) << std::endl;
  std::cout << "Size of vec: " << vec.size() << std::endl;
  std::cout << "Capacity of vec: " << vec.capacity() << std::endl;
  std::cout << "Distance between begin and end: " << std::distance(vec.begin(), vec.end()) << std::endl;

  return 0;
}

在这个示例中,使用 begin() 和 end() 获取了 vector 容器的首尾元素的迭代器,并使用 distance() 函数计算了它们之间的距离。此外,还使用了 size() 和 capacity() 函数获取 vector 容器的大小和容量。

五、itturnoutthat用法

有时,我们需要对一个容器执行一些类似过滤、查找、替换等操作。STL 为此提供了一系列专门的算法,包括 find、remove、replace、reverse、transform、sort 等等。在这些算法中,迭代器的应用非常广泛。

下面是一个使用 find 函数查找容器中特定元素的示例代码:

#include 
#include 
#include 

int main() {
  std::vector vec{1, 2, 3, 4, 5, 6, 7, 8, 9};
  auto it = std::find(vec.begin(), vec.end(), 3);
  if (it != vec.end()) {
    std::cout << "Found element: " << *it << std::endl;
  } else {
    std::cout << "Element not found." << std::endl;
  }
  return 0;
}

在这个示例中,使用了 find 函数查找 vector 容器中值为 3 的元素。如果找到了,就输出该元素的值,否则输出“Element not found.”。

六、iterator方法的功能

迭代器除了基本操作和接口,还有一些高级的方法能够提供更加强大的功能。

以下是一些示例代码:

#include 
#include 
#include 

int main() {
  std::vector vec{1, 2, 3, 4, 5};

  std::fill_n(vec.begin(), 3, 0); // 填充前三个元素为 0
  for (auto x : vec) {
    std::cout << x << " ";
  }
  std::cout << std::endl;

  std::vector vec2;
  vec2.resize(vec.size() * 2); // 扩大容器大小
  std::copy_backward(vec.begin(), vec.end(), vec2.end()); // 倒序复制元素
  for (auto x : vec2) {
    std::cout << x << " ";
  }
  std::cout << std::endl;

  return 0;
}

在这个示例中,使用了 fill_n 函数将 vector 容器前三个元素设置为 0,使用 resize 函数扩大了容器的大小,并使用 copy_backward 函数倒序复制原容器的元素到新的容器中。

七、iterator读法选取

在 C++ 中,迭代器通常是使用 auto 关键字进行类型推断的,而不是手动指定迭代器类型。

以下是一些示例代码:

#include 
#include 

int main() {
  std::vector vec{1, 2, 3, 4, 5};
 
  // 迭代器遍历 vector 容器
  for (auto it = vec.begin(); it != vec.end(); ++it) {
     std::cout << *it << " ";
  }
  std::cout << std::endl;

  return 0;
}

在这个示例中,使用 auto 关键字推断迭代器类型,并使用 *it 访问迭代器指向的元素。

结论

本文对 C++ 中的迭代器进行了详细的介绍,从基本操作、iteratorc 用法、itisbefore 用法、iterator 接口的用法、itturnoutthat 用法、iterator 方法的功能、iterator 读法选取等多个方面进行了阐述。通过阅读本文,相信读者已经对迭代器的使用有了更深入的理解和掌握。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/252140.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝的头像小蓝
上一篇 2024-12-13 17:34
下一篇 2024-12-13 17:34

相关推荐

  • Linux sync详解

    一、sync概述 sync是Linux中一个非常重要的命令,它可以将文件系统缓存中的内容,强制写入磁盘中。在执行sync之前,所有的文件系统更新将不会立即写入磁盘,而是先缓存在内存…

    编程 2025-04-25
  • 神经网络代码详解

    神经网络作为一种人工智能技术,被广泛应用于语音识别、图像识别、自然语言处理等领域。而神经网络的模型编写,离不开代码。本文将从多个方面详细阐述神经网络模型编写的代码技术。 一、神经网…

    编程 2025-04-25
  • MPU6050工作原理详解

    一、什么是MPU6050 MPU6050是一种六轴惯性传感器,能够同时测量加速度和角速度。它由三个传感器组成:一个三轴加速度计和一个三轴陀螺仪。这个组合提供了非常精细的姿态解算,其…

    编程 2025-04-25
  • Python安装OS库详解

    一、OS简介 OS库是Python标准库的一部分,它提供了跨平台的操作系统功能,使得Python可以进行文件操作、进程管理、环境变量读取等系统级操作。 OS库中包含了大量的文件和目…

    编程 2025-04-25
  • Java BigDecimal 精度详解

    一、基础概念 Java BigDecimal 是一个用于高精度计算的类。普通的 double 或 float 类型只能精确表示有限的数字,而对于需要高精度计算的场景,BigDeci…

    编程 2025-04-25
  • Linux修改文件名命令详解

    在Linux系统中,修改文件名是一个很常见的操作。Linux提供了多种方式来修改文件名,这篇文章将介绍Linux修改文件名的详细操作。 一、mv命令 mv命令是Linux下的常用命…

    编程 2025-04-25
  • git config user.name的详解

    一、为什么要使用git config user.name? git是一个非常流行的分布式版本控制系统,很多程序员都会用到它。在使用git commit提交代码时,需要记录commi…

    编程 2025-04-25
  • 详解eclipse设置

    一、安装与基础设置 1、下载eclipse并进行安装。 2、打开eclipse,选择对应的工作空间路径。 File -> Switch Workspace -> [选择…

    编程 2025-04-25
  • C语言贪吃蛇详解

    一、数据结构和算法 C语言贪吃蛇主要运用了以下数据结构和算法: 1. 链表 typedef struct body { int x; int y; struct body *nex…

    编程 2025-04-25
  • Python输入输出详解

    一、文件读写 Python中文件的读写操作是必不可少的基本技能之一。读写文件分别使用open()函数中的’r’和’w’参数,读取文件…

    编程 2025-04-25

发表回复

登录后才能评论