ex9.9

版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 4.0

Exercise 9.9

编写一个循环将 list 以逆序输出

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
#include <vector>
#include <list>
#include <deque>
#include <iostream>
#include <string>

using namespace std;

int main()
{

string s[] = { "hi", "hey", "hello" };
list<string> slist(s, s + 3);
list<string>::iterator iter = --slist.end();
int sz = sizeof(s) / sizeof(s[0]);
int cnt = 0;
while (cnt < sz) {
cout << (*iter) << endl;
if (++cnt < sz) {
--iter;
}
}
return 0;
}
// prints:
// hello
// hey
// hi