迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中取消引用迭代器

作者:迹忆客 最近更新:2023/08/29 浏览次数:

迭代器是 C++ 中的另一种强大机制,可帮助您迭代复杂的数据结构(例如树)和内部没有元素索引的集合(例如数组)。


C++ 中的迭代器是什么

在 C++ 中,迭代器是一个类似于指针的对象,指向数组、列表和任何其他数据结构中的元素。 我们使用迭代器来迭代容器元素; 尽管我们可以使用传统的循环,但迭代器在遍历容器时具有优势。

它提供了一种非常通用的方法来遍历容器的元素。

在像数组这样的一般数据结构中,我们可以使用索引变量来循环遍历容器,但是在像树或无序数据结构这样的高级数据结构中,我们没有索引变量来循环,我们使用迭代器来遍历它 。

容器向迭代器提供数据类型,容器类提供两个基本函数来帮助迭代器迭代容器的元素。

  1. Begin() - 它返回容器的第一个元素。
  2. End() - 它返回容器中最后一个元素的下一个元素。

让我们通过一个正确的代码示例来理解。

#include<iostream>
#include<vector>
using namespace std;

int main()
{
  // Declaring a vector
  vector<int> numbers = { 1, 2, 3, 4, 5 };

  // Declaring an iterator
  vector<int>::iterator i;

  int j;
  cout << "Traversing using loop = ";

  // Accessing the elements using loops
  for (j = 0; j < 5; ++j){
    cout << numbers[j] << ", ";
  }

  cout <<endl << "Traversing using Iterators = ";

  // Accessing the elements using iterators
  for (i = numbers.begin(); i != numbers.end(); ++i){
    cout << *i << ", ";
  }

  // Adding another element to the vector
  numbers.push_back(6);

  cout <<endl << "Traversing using loops after adding new value to numbers = ";

    //After adding a new value to the numbers vector
    for (j = 0; j < 5; ++j){
     cout << numbers[j] << ", ";
  }

  cout <<endl << "Traversing using iterators after adding new value to numbers = ";

  // Accessing the elements using iterators
  for (i = numbers.begin(); i != numbers.end(); ++i){
      cout << *i << ", ";
  }
  return 0;
}

输入:

Traversing using loop = 1, 2, 3, 4, 5,
Traversing using Iterators = 1, 2, 3, 4, 5,
Traversing using loops after adding new value to numbers = 1, 2, 3, 4, 5,
Traversing using iterators after adding new value to numbers = 1, 2, 3, 4, 5, 6,

该程序演示了使用循环和迭代器遍历容器的用法之间的差异。 在上面的代码中,迭代器跟踪代码并动态地采用代码中的任何更改。

在循环中,您需要静态更新循环代码。 迭代器正在实现代码可重用性因素。

在迭代器中,begin() 函数获取容器的第一个元素,end() 函数获取最后一个元素的下一个元素。


C++ 中的取消引用

C++ 中的解引用运算符用于访问或操作指针指向的内存位置中的数据。 * 星号与指针变量一起使用,对指针变量进行求差,它引用的是指向变量,这称为指针的解引用。

代码示例:

#include<iostream>
using namespace std;

int main() {
   int a = 9, b;
   int *p; // Un-initialized Pointer
   p = &a; // Store the address of a in pointer p
   b = *p; // Put the Value  of the pointer p in b
   cout<<"The value of a        =   "<<a<<endl;
   cout<<"The value of pointer p =   "<<*p<<endl;
   cout<<"The value of b        =   "<<b<<endl;
}

输出:

The value of a        =   9
The value of pointer p =   9
The value of b        =   9

我们首先将 9 赋给 a,然后定义一个指针 p。 接下来,我们将a的地址分配给p,这是一个指针,最后,我们将指针分配给整数数据类型的变量b。

现在,在显示时,a、p 和 b 得到与 9 相同的输出,因为 p 具有 a 的地址,这有助于我们在任何地方显示 a 的值。


为什么我们不能在 C++ 中取消引用迭代器

在 C++ 中,不能立即取消引用迭代器,因为 end() 函数返回迭代器和对象作为指针,而指针不是数据结构的有效成员。

当您在末尾取消引用时,它会引发错误,因为结束指针的目的只是查看何时到达它。

Begin()返回容器不为空时的位置,换句话说,满足这个条件。

v.begin() != v.end()  // this condition checks, if the container is empty or not

但如果您需要访问最后一个元素,可以使用下面的代码。

vector<object>::const_iterator i = vectorOfObjects.end();
i--;
cout << *i << endl; // this prints the last element of the container

提醒一下,此代码仅在向量包含至少一个元素时才有效。

代码示例:

#include <iostream>
#include <string>
#include <vector>

using namespace std;
struct Student {
int roll;
string name;
int age;
};

int main(){

Student s1, s2, s3;                // Objects of strucu
vector<Student> ListOfStudents;

s1.roll = 1;                    //populating the data members of Student
s2.roll = 2;
s3.roll = 3;

s1.name = "Mike";
s2.name = "John";
s3.name = "Tom";

s1.age = 15;
s2.age = 16;
s3.age = 14;

ListOfStudents.push_back(s1);
ListOfStudents.push_back(s2);
ListOfStudents.push_back(s3);

vector<Student>::const_iterator iter = ListOfStudents.begin(); //begining position

while ( iter != ListOfStudents.end()){ // if the container is empty or not
cout << "Roll number " << iter->roll << " is "
        <<(*iter).name << " and he is "
        << iter->age << " years old." << endl;
++iter;
}
return 0;
}

输出:

Roll number 1 is Mike and he is 15 years old.
Roll number 2 is John and he is 16 years old.
Roll number 3 is Tom and he is 14 years old.

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便