'Accessing element from list
How can I access particular element from a list in C++? Basically programm would display list and ask to chose one element that would be moved to the end. For example there would be list: 2 4 5 8 3 4 if input would be 5 then result would be 2 4 8 3 4 5. Current code works only to move first element to the end. How to access any element from list and to move it to the end?
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
void printLists ( const list<int>& l2)
{
cout << "list1: ";
copy (l2.begin(), l2.end(), ostream_iterator<int>(cout," "));
cout << endl;
}
int main()
{
// create empty list
list<int> list1;
// fill list with elements
for (int i=0; i<6; ++i) {
list1.push_front(i);
}
printLists( list1);
// move first element to the end
list1.splice(list1.end(), // destination position
list1, // source list
list1.begin()); // source position
printLists(list1);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
