STL : Vector [6] (erase, insert, swap)


/* STL : Vector [6] (erase, insert, swap)
Author : Tausiq
B.Sc in CTE (Studying)
United International University
*/

#include
#include
using namespace std;

int main ()
{
vector v (5);

for ( int i = 0; i < 5; i++ ) v [i] = i * 2; // 0 2 4 6 8 vector :: iterator p;

p = v.begin ();
p += 3; // points 4th element

v.erase (p); // erase pointed element

for ( unsigned int i = 0; i < v.size (); i++ ) cout << v [i] << ' '; // 0 2 4 8 cout << endl; v.insert (p, 5); /* p points at 4th element and 5 inserted as 4th element */ for ( unsigned int i = 0; i < v.size (); i++ ) cout << v [i] << ' '; // 0 2 4 5 8 cout << endl; vector v2 (5, 0);

v2.swap (v);

for ( unsigned int i = 0; i < v.size (); i++ ) cout << v [i] << ' '; // 0 0 0 0 0 cout << endl; for ( unsigned int i = 0; i < v2.size (); i++ ) cout << v2 [i] << ' '; // 0 2 4 5 8 cout << endl; return 0; } [/sourcecode]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s