C++ list(STL list)访问(获取)元素方法详解
正如我们所了解的那样,begin() 和 end() 分别返回的是指向第一个和最后一个元素下一个位置的双向迭代器。rbegin() 和 rend() 函数返回的双向迭代器,可以让我们逆序遍历元素。因为可以对 list 使用基于范围的循环,所以当我们想要处理所有元素时,可以不使用迭代器:
std::list<std::string> names {"Jane","Jim", "Jules", "Janet"}; names.emplace_back("Ann"); std::string name("Alan"); names.emplace_back(std::move(name)); names.emplace_front("Hugo"); names.emplace(++begin(names), "Hannah"); for(const auto& name : names) std::cout << name << std::endl;循环变量 name 是依次指向每个 list 元素的引用,使得循环能够逐行输出各个字符串。下面通过一个练习检验一下前面学到的知识。这个练习读取通过键盘输入的读语并将它们存储到一个 list 容器中:
// Working with a list #include <iostream> #include <list> #include <string> #include <functional> using std::list; using std::string; // List a range of elements template<typename Iter> void list_elements(Iter begin, Iter end) { while (begin != end) std::cout << *begin++ << std::endl; } int main() { std::list<string> proverbs; // Read the proverbs std::cout << "Enter a few proverbs and enter an empty line to end:" << std::endl; string proverb; while (getline(std::cin, proverb, '\n'), !proverb.empty()) proverbs.push_front(proverb); std::cout << "Go on, just one more:" << std::endl; getline(std::cin, proverb, '\n'); proverbs.emplace_back(proverb); std::cout << "The elements in the list in reverse order are:" << std::endl; list_elements(std::rbegin(proverbs), std::rend(proverbs)); proverbs.sort(); // Sort the proverbs in ascending sequence std::cout << "\nYour proverbs in ascending sequence are:" << std::endl; list_elements(std::begin(proverbs), std::end(proverbs)); proverbs.sort(std::greater<>()); // Sort the proverbs in descending sequence std::cout << "\nYour proverbs in descending sequence:" << std::endl; list_elements(std::begin(proverbs), std::end(proverbs)); }运行结果为:
Enter a few proverbs and enter an empty line to end: A nod is a good as a wink to a blind horse.
Many a mickle makes a muckle.
A wise man stands on the hole in his carpet.
Least said, soonest mended.
Go on, just one more:
A rolling stone gathers no moss.
The elements in the list in reverse order are:
A rolling stone gathers no moss.
A nod is a good as a wink to a blind horse.
Many a mickle makes a muckle.
A wise man stands on the hole in his carpet.
Least said, soonest mended.
Your proverbs in ascending sequence are:
A nod is a good as a wink to a blind horse.
A rolling stone gathers no moss.
A wise man stands on the hole in his carpet.
Least said, soonest mended.
Many a mickle makes a muckle.
Your proverbs in descending sequence:
Many a mickle makes a muckle.
Least said, soonest mended.
A wise man stands on the hole in his carpet.
A rolling stone gathers no moss.
A nod is a good as a wink to a blind horse.
第一次调用 proverbs 的成员函数 sort() 时,没有提供参数,因此元素被默认排成升序。第二次调用时,提供了一个 greater 断言作为参数;这个模板和其他几个会在后面遇到的标准断言模板都定义在头文件 functional 中。表达式 greater<>() 定义了一个函数对象,这个函数对象可以用 opemtor>() 来比较对象,推导模板参数类型。结果,list 中的元素变成了降序排列。还有其他一些对象,它们也定义了一些 sort() 会经常用到的断言,包括 greater_equal<>()、less<>() 和 less_equal<>()。从名称就可以看出它们是如何进行比较的。从这个示例的输入来看,一切都很符合我们的预期。
所有教程
- socket
- Python基础教程
- C#教程
- MySQL函数
- MySQL
- C语言入门
- C语言专题
- C语言编译器
- C语言编程实例
- GCC编译器
- 数据结构
- C语言项目案例
- C++教程
- OpenCV
- Qt教程
- Unity 3D教程
- UE4
- STL
- Redis
- Android教程
- JavaScript
- PHP
- Mybatis
- Spring Cloud
- Maven
- vi命令
- Spring Boot
- Spring MVC
- Hibernate
- Linux
- Linux命令
- Shell脚本
- Java教程
- 设计模式
- Spring
- Servlet
- Struts2
- Java Swing
- JSP教程
- CSS教程
- TensorFlow
- 区块链
- Go语言教程
- Docker
- 编程笔记
- 资源下载
- 关于我们
- 汇编语言
- 大数据
- 云计算
- VIP视频