You need to solve the following problem recently:
Read all the content in a list and output it (for example, to the screen). The first and last rows are output only once, and all the rows in the middle are output twice. For example, a list contains the following content:
1
2
3
4
Output it to the screen. The expected result is as follows:
1
2
2
3
3
4
I implemented it using the following program:
1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 list<int> intList;
9 list<int>::iterator intIterator;
10
11 for (int i = 0; i < 4; i++)
12 {
13 intList.push_back(i + 1);
14 }
15 for (intIterator = intList.begin(); intIterator != intList.end(); intIterator++)
16 {
17 if (intIterator == intList.begin())
18 {
19 cout << *intIterator << "\t";
20 } else {
21 cout << *intIterator << endl;
22 intIterator++;
23 if (intIterator != intList.end())
24 {
25 intIterator--;
26 cout << *intIterator << "\t";
27 } else {
28 break;
29 }
30 }
31 }
32
33 return 0;
34 }