Friday, January 1, 2010

STL LIST- SPLICE

Move elements from list to list

Moves elements from list x into the list container at the specified position, effectively inserting the specified elements into the container and removing them from x.

This increases the container size by the amount of elements inserted, and reduces the size of x by the same amount ( whenever x is not the same as *this ).

The operation does not involve the construction or destruction of any element object and, except for the third version, it is performed in constant time.

The iterators that pointed to moved elements are no longer valid.


// splicing lists
#include < iostream >
#include < list >
using namespace std;

int main ()
{
list< int > mylist1, mylist2;
list::iterator it;

// set some initial values:
for (int i=1; i<=4; i++)
mylist1.push_back(i); // mylist1: 1 2 3 4

for (int i=1; i<=3; i++)
mylist2.push_back(i*10); // mylist2: 10 20 30

it = mylist1.begin();
++it; // points to 2

mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
// mylist2 (empty)
// "it" still points to 2 (the 5th element)

mylist2.splice (mylist2.begin(),mylist1, it);
// mylist1: 1 10 20 30 3 4
// mylist2: 2
// "it" is now invalid.
it = mylist1.begin();
advance(it,3); // "it" points now to 30

mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
// mylist1: 30 3 4 1 10 20

cout << "mylist1 contains:";
for (it=mylist1.begin(); it!=mylist1.end(); it++)
cout << " " << *it;

cout << "\nmylist2 contains:";
for (it=mylist2.begin(); it!=mylist2.end(); it++)
cout << " " << *it;
cout << endl;

return 0;
}

Output:
mylist1 contains: 30 3 4 1 10 20
mylist2 contains: 2

STL LIST C++ -- insert(), erase() & assign()

insert:

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::iterator it;

// 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 myvector (2,30);
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

STL LIST- SWAP()

list::swap

Swap content

Exchanges the content of the vector by the content of lst, which is another list object containing elements of the same type. Sizes may differ.

// swap lists
#include
#include
using namespace std;

main ()
{
list first (3,100);
// three ints with a value of 100
list second (5,200);
// five ints with a value of 200
list::iterator it;

first.swap(second);

cout << "first contains:";
for (it=first.begin(); it!=first.end(); it++) cout << " " << *it;

cout << "\nsecond contains:";
for (it=second.begin(); it!=second.end(); it++) cout << " " << *it;

cout << endl;

return 0;
}


Output:
first contains: 200 200 200 200 200
second contains: 100 100 100

STL LIST - clear(), sort() and unique()

clear():
void clear ( );
Clear content

All the elements in the list container are dropped: their destructors are called, and then they are removed from the list container, leaving it with a size of 0.

// clearing lists
#include
#include
using namespace std;

int main ()
{
list mylist;
list::iterator it;

mylist.push_back (100);
mylist.push_back (200);
mylist.push_back (300);

cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;

mylist.clear();
mylist.push_back (1101);
mylist.push_back (2202);

cout << "\nmylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;

cout << endl;

return 0;
}

Output:
mylist contains: 100 200 300
mylist contains: 1101 2202


sort():

Sort elements in container.
Sorts the elements in the container from lower to higher. The sorting is performed by comparing the elements in the container in pairs using a sorting algorithm.

int main ()
{
list mylist;
list::iterator it;
mylist.push_back ("one");
mylist.push_back ("two");
mylist.push_back ("Three");

mylist.sort();

cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
Output:
mylist contains: Three one two

For default strings, the comparison is a strict character code comparison, where all uppercase letters compare lower than all lowercase letters, putting all strings beginning by an uppercase letter before in the first sorting operation.



unique():
Remove duplicate values

int main ()
{
double mydoubles[]={ 12.15, 2.72, 73.0, 12.77, 3.14,
12.77, 73.35, 72.25, 15.3, 72.25 };
list mylist (mydoubles,mydoubles+10);

mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77,
// 15.3, 72.25, 72.25, 73.0, 73.35

mylist.unique(); // 2.72, 3.14, 12.15, 12.77
// 15.3, 72.25, 73.0, 73.35
cout << "mylist contains:";
for (list::iterator it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;
cout << endl;

return 0;
}

Output:
mylist contains:2.72, 3.14, 12.15, 12.77, 15.3, 72.25, 73.0, 73.35

STL LIST C++

List

header:

Lists are a kind of sequence containers. As such, their elements are ordered following a linear sequence.

List containers are implemented as doubly-linked lists; Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept by the association to each element of a link to the element preceding it and a link to the element following it.

This provides the following advantages to list containers:
1.Efficient insertion and removal of elements anywhere in the container (constant time).
2.Efficient moving elements and block of elements within the container or even between different containers (constant time).
3.Iterating over the elements in forward or reverse order (linear time).


Compared to other base standard sequence containers (vectors and deques), lists perform generally better in inserting, extracting and moving elements in any position within the container, and therefore also in algorithms that make intensive use of these, like sorting algorithms.

The main drawback of lists compared to these other sequence containers is that they lack direct access to the elements by their position; For example, to access the sixth element in a list one has to iterate from a known position (like the beginning or the end) to that position, which takes linear time in the distance between these. They also consume some extra memory to keep the linking information associated to each element (which may be an important factor for large lists of small-sized elements).

eg.

list Obj; //declared an int list
list::iterator itr= Obj.begin(); //declared an iterator to int list

STL ITERATORS- ADVANCE & DISTANCE

ADVANCE:
Belongs to

Advance iterator
Advances the iterator i by n elements.

If i is a Random Access Iterator, the function uses once operator+ or operator-, otherwise, the function uses repeatedly the increase or decrease operator (operator++ or operator--) until n elements have been advanced.

Return value:none

// advance example
#include
#include
#include
using namespace std;

int main () {
list mylist;
for (int i=0; i<10; i++) mylist.push_back (i*10);

list::iterator it = mylist.begin();

advance (it,5);

cout << "The sixth element in mylist is: " << *it << endl;

return 0;
}
OUTPUT:
The sixth element in mylist is: 50



DISTANCE:
Return distance between iterators

Calculates the number of elements between first and last.

If i is a Random Access Iterator, the function uses operator- to calculate this. Otherwise, the function uses repeatedly the increase or decrease operator (operator++ or operator--) until this distance is calculated.

Return value:
The number of increments or decrements needed to get from first to last.

// distance example
#include
#include
#include
using namespace std;

int main () {
list mylist;
for (int i=0; i<10; i++) mylist.push_back (i*10);

list::iterator first = mylist.begin();
list::iterator last = mylist.end();

cout << "The distance is: " << distance(first,last) << endl;

return 0;
}

Output

The distance is: 10

Sunday, December 27, 2009

How would you get the character positions 10-20 from a text file?

How would you get the character positions 10-20 from a text file?

cut -c10-20

Or

substring ${string_variable_name:starting_position:length}

Or

cat filename.txt | cut -c 10-20