STL : max_element () + min_element ()


#include
#include
#include
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 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. */ [/sourcecode]

Advertisements

STL: Algorithm: generate ()


#include
#include
using namespace std;

int main ()
{
vector 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. */ [/sourcecode]

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
#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]

STL : Vector [5] (empty, pop_back)


/* STL : Vector [5] (empty, pop_back)
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; 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; } [/sourcecode]

STL : Vector [4] (begin, end)


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

#include
#include
using namespace std;

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

vector :: 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; } [/sourcecode]