- C++ 迭代器是一种用于访问容器(例如数组、向量、列表等)元素的工具。它们允许我们以一种统一的方式遍历和操作容器中的数据,而不用关心容器内部数据结构的细节
C++ 迭代器
std 重要函数
- std::begin 和 std::end
- std::advance(iter,n)向前移动
- std::distance(one,two) 计算两个迭代器之间的距离
- std::next 和 std::prev 用于获取给定迭代器的下一个或上一个迭代器
- std::copy 用于将一个容器的元素复制到另一个容器中
- std::fiind 用于在容器中查找指定的值,并返回找到的第一个匹配元素的迭代器
- std::sort 用于对容器中的元素进行排序
- 默认使用升序排序
- std::greater<int>()
- std::reverse 用于反转容器中元素的顺序
next
- 对于任何可迭代的容器,它都支持至少以下两个基本操作:
- begin()返回指向容器中第一个元素的迭代器
- end()返回指向容器中最后一个元素之后位置的迭代器
- next 是一个标准库函数,它接受一个迭代器作为参数,并返回一个新的迭代器,该迭代器指向参数迭代器之后的位置。而++迭代器则将迭代器引用自身向前移动一个位置
sort
- std::sort(numbers.begin(), numbers.end());
- std::sort(numbers.begin(), numbers.end(), std::greater<int>());
C++ 迭代器与 vector
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
std::cout << "P seq:" << std::endl;
for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it)
{
std::cout << *it << " " << std::endl;
}
std::cout << "N seq:" << std::endl;
for (std::vector<int>::reverse_iterator rit = numbers.rbegin(); rit != numbers.rend(); ++rit)
{
std::cout << *rit << " " << std::endl;
}
return 0;
}
- example
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = { 1, 2, 3, 4, 5 };
std::vector<int>::iterator it = nums.begin();
std::vector<int>::iterator endIt = nums.end();
while (it != endIt)
{
std::cout << *it << " ";
++it;
}
return 0;
}
- example 增,删,改,查vector 中的元素
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = { 1, 2, 3, 4, 5 };
std::cout << "original vector:(ergodic)";
for (std::vector<int>::iterator it = nums.begin(); it != nums.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
// add
nums.push_back(6);
std::cout << "vector (push_back(6)):";
for (std::vector<int>::iterator it = nums.begin(); it != nums.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
// delete
std::vector<int>::iterator delit = nums.begin() + 2; // 删除第3个元素
nums.erase(delit);
std::cout << "vector:(erase(delit))";
for (std::vector<int>::iterator it = nums.begin(); it != nums.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
// modify
std::vector<int>::iterator modifyit = nums.begin() + 1; // 修改第2个元素
*modifyit = 10;
std::cout << "vector:(use pointer here)";
for (std::vector<int>::iterator it = nums.begin(); it != nums.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
// find
int target = 4;
std::vector<int>::iterator findit = std::find(nums.begin(), nums.end(), target);
std::cout << "findit " << *findit << std::endl;
if (findit != nums.end())
{
std::cout << "find " << target << std::endl;
}
else {
std::cout << "not find " << target << std::endl;
}
return 0;
}
C++ 迭代器与 数组
- 数组是一种用于存储相同类型元素的容器。它可以包含任意数量的元素,并通过索引访问每个元素
#include <iostream>
int main() {
int array[5] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++)
{
std::cout << "Element at index " << i << ": " << array[i] << std::endl;
}
return 0;
}
- string 类型
- auto 用于声明一个自动推断类型的迭代器
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
for (auto it = str.begin(); it != str.end(); ++it)
{
std::cout << *it;
}
std::cout << std::endl;
return 0;
}
C++ 迭代器与 列表
- 创建一个空列表
- 向列表中添加元素(在末尾、头部和指定位置插入)
- 遍历并打印列表中的元素
- 访问和修改列表中的元素(访问第一个和最后一个元素)
- 删除列表中的元素(删除头部和指定位置的元素)
- 检查列表是否为空
- 清空列表
#include <iostream>
#include <list>
int main() {
std::list<int> myList;
myList.push_back(10);
myList.push_back(20);
myList.push_back(30);
std::list<int>::iterator it;
for (it = myList.begin(); it != myList.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
std::list<int>::iterator insertPos = ++myList.begin();
myList.insert(insertPos, 15);
for (it = myList.begin(); it != myList.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
std::list<int>::iterator deletePos = myList.begin();
++deletePos;
++deletePos;
myList.erase(deletePos);
for (it = myList.begin(); it != myList.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
- cpp 中列表的基本操作示例
- myList.push_back(5);
myList.push_front(3); - myList.erase()
#include <iostream>
#include <list>
int main() {
std::list<int> myList;
myList.push_back(5);
myList.push_front(3);
myList.insert(std::next(myList.begin()), 4);
for (const auto& num : myList)
{
std::cout << num << " ";
}
std::cout << std::endl;
std::cout << "First element: " << myList.front() << std::endl;
std::cout << "Last element: " << myList.back() << std::endl;
myList.pop_front();
myList.erase(std::next(myList.begin()));
for (const auto& num : myList)
{
std::cout << num << " ";
}
std::cout << std::endl;
if (myList.empty()) {
std::cout << "The list is empty." << std::endl;
}
myList.clear();
if (myList.empty()) {
std::cout << "The list is empty." << std::endl;
}
return 0;
}
实际应用案例
迭代器运行时间计时
- #include <chrono>
- auto start = std::chrono::high_resolution_clock::now();
- auto end = std::chrono::high_resolution_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
#include <iostream>
#include <vector>
#include <chrono>
int main() {
std::vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto start = std::chrono::high_resolution_clock::now();
for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it)
{
std::cout << *it << " ";
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "\n\n程序执行时间: " << duration << " 微秒" << std::endl;
return 0;
}
计数器
- count++
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int count = 0;
for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it)
{
count++;
}
std::cout << "count:" << count << std::endl;
return 0;
}