STL : max_element () + min_element ()


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main ()
{
    int x [] = {2, 7, 92, 81, 19, 72, 23};

    unsigned int array_length = sizeof (x) / sizeof (int);

    cout << "Maximum : " << *max_element (x, x + array_length) << endl;

    vector <char> v;

    v.push_back ('k');
    v.push_back ('f');
    v.push_back ('g');
    v.push_back ('b');

    cout << "Minimum : " << *min_element ( v.begin (), v.end () ) << endl;

    return 0;
}

/* output
Maximum : 92
Minimum : b

Process returned 0 (0x0)   
execution time : 0.078 s
Press any key to continue.
*/


STL: Algorithm: generate ()


#include <algorithm>
#include <vector>
using namespace std;

int main ()
{
    vector <int> v (10);
    // allocate space for 10 elements

    generate( v.begin (), v.end (), rand);
    // generate random numbers

    for ( size_t i = 0; i < v.size (); i++ )
        printf ("%d\n", v [i]);

    return 0;
}

/* output
41
18467
6334
26500
19169
15724
11478
29358
26962
24464

Process returned 0 (0x0)
execution time : 0.031 s
Press any key to continue.
*/


STL: Algorithm: binary_search ()


#include <algorithm>
#include <vector>
using namespace std;

int main ()
{
    int array [] = {5, 4, 1, 7, 9, 13, 32, 5, 71};
    int array_length = 9;

    vector <int> v ( array, array + array_length );
    // 5 4 1 7 9 13 32 5 71

    sort ( v.begin (), v.end () );
    // 1 4 5 5 7 9 13 32 71

    int key = 9;

    // binary_search() runs in logarithmic time
    if ( binary_search ( v.begin (), v.end (), key ) )
        printf ("key : %d found\n", key);
    else
        printf ("key : %d not found\n", key);

    key = 10;

    if ( binary_search ( v.begin (), v.end (), key ) )
        printf ("key : %d found\n", key);
    else
        printf ("key : %d not found\n", key);

    return 0;
}
/* output
key : 9 found
key : 10 not found

Process returned 0 (0x0)   
execution time : 0.046 s
Press any key to continue.
*/

STL: Algorithm: prev_permutation ()


#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

int main ()
{
    vector <int> v;

    v.push_back (2);
    v.push_back (1);
    v.push_back (3);
    v.push_back (1);

    sort ( v.begin (), v.end (), greater <int> () );
    // descending sort, header : functional

    do {
        printf ("%d %d %d %d \n", v [0], v [1], v [2], v [3]);
    } while ( prev_permutation ( v.begin (), v.end () ) );

    return 0;
}

/* output
3 2 1 1
3 1 2 1
3 1 1 2
2 3 1 1
2 1 3 1
2 1 1 3
1 3 2 1
1 3 1 2
1 2 3 1
1 2 1 3
1 1 3 2
1 1 2 3

Process returned 0 (0x0)   
execution time : 0.031 s
Press any key to continue.
*/


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


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

#include <iostream>
#include <vector>
using namespace std;

int main ()
{
    vector <int> v (5);

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

    vector <int> :: 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 <int> 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;
}


STL : Vector [5] (empty, pop_back)


/* STL : Vector [5] (empty, pop_back)
Author : Tausiq
B.Sc in CTE (Studying)
United International University
*/

#include <iostream>
#include <vector>
using namespace std;

int main ()
{
    vector <int> v (5);

    for ( int i = 0; i < 5; i++ )
        v [i] = i * 2;

    for ( int i = 0; i < 5; i++ )
        cout << v [i] << ' ';
    cout << endl;

    while ( v.empty () != true ) {
        cout << "size: " << v.size () << endl;
        v.pop_back ();
    }

    return 0;
}

STL : Vector [4] (begin, end)


/* STL : Vector [4] (begin, end)
Author : Tausiq
B.Sc in CTE (Studying)
United International University
*/

#include <iostream>
#include <vector>
using namespace std;

int main ()
{
    vector <int> v (5, 1);
    /* makes a vector of length 5
    and fill the vector with value 1 */

    vector <int> :: iterator i;
    /* iterator i works like a pointer */

    i = v.begin ();
    /* now 'i' points the beginning of
    the vector v */

    while ( i < v.end () ) {
        cout << *i << ' ';
        i++;
    }

    cout << endl;

    /* the process can be written
    alternatively like this */

    i = v.end () - 1;

    while ( i >= v.begin () ) {
        cout << *i << ' ';
        i--;
    }

    cout << endl;

    return 0;
}


STL : Vector [3] (begin, size, clear)


/* STL : Vector [3] (begin, size, clear)
Author : Tausiq */

#include <stdio.h>
#include <vector>
using namespace std;

int main ()
{
	vector <int> v;
	
	int i;
	for ( i = 10; i < 50; i += 10 )
		v.push_back ( i ); // 10 20 30 40

	printf ("begin : %d\n", *v.begin () );

	printf ("size : %d\n", v.size () );

	v.clear (); // make empty the vector

	printf ("size : %d\n", v.size () );

	return 0;
}

/*
output : 
begin : 10
size : 4
size : 0
*/

STL : Vector [2] (push_back, at, back, front)


/* STL : Vector [2] (push_back, at, back, front)
Author : Tausiq
B.Sc in CTE 
United International University
*/

#include "stdio.h"
#include "vector"
using namespace std;

int main ()
{
	vector <char> v;
	int i;

	for ( i = 0; i < 5; i++ )
		v.push_back ( i + 65 );

	for ( i = 0; i < 5; i++ ) 
		printf ("%d : %c\n", i + 1, v.at (i) ); // A B C D E

	printf ("Last Element : %c\n", v.back () ); // E
	printf ("First Element : %c\n", v.front () ); // A

	return 0;
}

STL : Vector [1] (Introduction)

Vector : (Introduction)

Vector : (Introduction)

:: Vector :: ( Introduction ) ::

Follow

Get every new post delivered to your Inbox.

Join 48 other followers