Example details of C++ list
- 2020-05-30 20:52:03
- OfStack
Example details of C++ list
Source:
#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>
using namespace std;
typedef list<int> LISTINT; // create 1 a list Instances of containers LISTINT
typedef list<int> LISTCHAR; // create 1 a list Instances of containers LISTCHAR
int main(void) {
LISTINT listOne; // with LISTINT create 1 called listOne the list object
LISTINT::iterator i; // The statement i For the iterator
listOne.push_front (2); // Once upon a time for listOne Add data to the container
listOne.push_front (1);
listOne.push_back (3); // From the face listOne Add data to the container
listOne.push_back (4);
cout<<"listOne.begin()--- listOne.end():"<<endl; // Front to back listOne The data in the
for (i = listOne.begin(); i != listOne.end(); ++i)
cout << *i << " ";
cout << endl;
LISTINT::reverse_iterator ir; // Display from the back to the back listOne The data in the
cout<<"listOne.rbegin()---listOne.rend():"<<endl;
for (ir =listOne.rbegin(); ir!=listOne.rend();ir++)
cout << *ir << " ";
cout << endl;
int result = accumulate(listOne.begin(), listOne.end(),0); // use STL the accumulate( cumulative ) algorithm
cout<<"Sum="<<result<<endl;
LISTCHAR listTwo; // with LISTCHAR create 1 called listOne the list object
LISTCHAR::iterator j; // The statement j For the iterator
listTwo.push_front ('A'); // Once upon a time for listTwo Add data to the container
listTwo.push_front ('B');
listTwo.push_back ('x'); // From the face listTwo Add data to the container
listTwo.push_back ('y');
cout<<"listTwo.begin()---listTwo.end():"<<endl; // Front to back listTwo The data in the
for (j = listTwo.begin(); j != listTwo.end(); ++j)
cout << char(*j) << " ";
cout << endl;
// use STL the max_element Algorithm for listTwo The largest element in and displays
j=max_element(listTwo.begin(),listTwo.end());
cout << "The maximum element in listTwo is: "<<char(*j)<<endl;
return 0;
}
Result:
[work@db-testing-com06-vm3.db01.baidu.com c++]$ g++ -o list list.cpp
[work@db-testing-com06-vm3.db01.baidu.com c++]$ ./list
listOne.begin()--- listOne.end():
1 2 3 4
listOne.rbegin()---listOne.rend():
4 3 2 1
Sum=10
listTwo.begin()---listTwo.end():
B A x y
The maximum element in listTwo is: y
If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!