list访问(获取)元素 图片看不了?点击切换HTTP 返回上层
正如我们所了解的那样,begin() 和 end() 分别返回的是指向第一个和最后一个元素下一个位置的双向迭代器。rbegin() 和 rend() 函数返回的双向迭代器,可以让我们逆序遍历元素。因为可以对 list 使用基于范围的循环,所以当我们想要处理所有元素时,可以不使用迭代器:
1 2 3 4 5 6 7 | 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; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | // 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<>()。从名称就可以看出它们是如何进行比较的。从这个示例的输入来看,一切都很符合我们的预期。