Insert elements
The list container is extended by inserting new elements before the element at position.
This effectively increases the container size by the amount of elements inserted.
lists are sequence containers specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence. Compared to the other base sequence containers (vector and deque), lists are the most efficient container doing insertions at some position other than the beginning or the end of the sequence, and, unlike in these, all of the previously obtained iterators and references remain valid after the insertion and refer to the same elements they were referring before.
#include < iostream >
#include < list >
#include < vector >
using namespace std;
int main ()
{
list < int > mylist;
list
// set some initial values:
for (int i=1; i<=5; i++) mylist.push_back(i); // 1 2 3 4 5
it = mylist.begin();
++it; // it points now to number 2 ^
mylist.insert (it,10); // 1 10 2 3 4 5
// "it" still points to number 2 ^
mylist.insert (it,2,20); // 1 10 20 20 2 3 4 5
--it; // it points now to the second 20 ^
vector
mylist.insert (it,myvector.begin(),myvector.end());
// 1 10 20 30 30 20 2 3 4 5
// ^
cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); it++)
cout << " " << *it;
cout << endl;
return 0;
}
Output:
mylist contains: 1 10 20 30 30 20 2 3 4 5
erase:
Erase elements
Removes from the list container either a single element (position) or a range of elements ([first,last)).
This effectively reduces the list size by the number of elements removed, calling each element's destructor before.
lists are sequence containers specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence. Compared to the other base sequence containers (vector and deque), lists are the most efficient container erasing at some position other than the beginning or the end of the sequence, and, unlike in these, all of the previously obtained iterators and references remain valid after the erasing operation and refer to the same elements they were referring before (except, naturally, for those referring to erased elements).
// erasing from list
#include < iostream >
#include < list >
using namespace std;
int main ()
{
unsigned int i;
list< unsigned int > mylist;
list< unsigned int >::iterator it1,it2;
// set some values:
for (i=1; i<10; i++) mylist.push_back(i*10);
// 10 20 30 40 50 60 70 80 90
it1 = it2 = mylist.begin(); // ^^
advance (it2,6); // ^ ^
++it1; // ^ ^
it1 = mylist.erase (it1); // 10 30 40 50 60 70 80 90
// ^ ^
it2 = mylist.erase (it2); // 10 30 40 50 60 80 90
// ^ ^
++it1; // ^ ^
--it2; // ^ ^
mylist.erase (it1,it2); // 10 30 60 80 90
// ^
cout << "mylist contains:";
for (it1=mylist.begin(); it1!=mylist.end(); ++it1)
cout << " " << *it1;
cout << endl;
return 0;
}
Output:
mylist contains: 10 30 60 80 90
assign:
Assign new content to container
Assigns new content to the container, dropping all the elements contained in the container object before the call and replacing them by those specified by the parameters:
// list::assign
#include < iostream >
#include < list >
using namespace std;
int main ()
{
list< int > first;
list< int > second;
first.assign (7,100); // 7 ints with value 100
second.assign (first.begin(),first.end()); // a copy of first
int myints[]={1776,7,4};
first.assign (myints,myints+3); // assigning from array
cout << "Size of first: " << int (first.size()) << endl;
cout << "Size of second: " << int (second.size()) << endl;
return 0;
}
Output:
Size of first: 3
Size of second: 7
No comments:
Post a Comment