Standard Template Librar in C++ --- STL
The Standard Template Library provides a set of well structured
generic C++ components that work together in a seamless way.
Enables generic programming in C++
– Each generic algorithm can operate over any iterator for which the
necessary operations are provided
– Extensible: can support new algorithms, containers, iterators
– Sequential: vector, deque, list
– Associative: set, multiset, map, multimap
– Adapters: stack, queue, priority queue
– Input, output, forward, bidirectional, & random access
– Each container declares a trait for the type of iterator it provides
– Mutating, non-mutating, sorting, & numeric
Vector
vector is a dynamic
array that can grow & shrink
at the end
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <string>
using namespace std;
int main (int argc, char *argv[])
{
vector <string> projects;
cout << "program name:"<< argv[0] << endl;
for (int i = 1; i < argc; i++) {
projects.push_back (argv [i]);
cout << projects [i - 1]<< endl;
}
system("pause");
return 0;
}
OUTPUT: arg1 arg2 arg3
Deque
deque (pronounced “deck”) is a double-ended
queue
It adds efficient insertion& removal at the beginning& end of the sequence via
push front()& pop front()
#include <iostream>
#include <iomanip>
#include <cmath>
#include <deque>
#include <string>
using namespace std;
int main (int argc, char *argv[])
{
deque<int> a_deck; a_deck.push_back (3); a_deck.push_front (1); a_deck.insert (a_deck.begin () + 1, 2); a_deck[2] = 0; copy (a_deck.begin (), a_deck.end (), ostream_iterator<int> (cout, " "));
system("pause");
return 0;
}
OUTPUT: 1 2 0
List
list has
constant time
insertion & deletion at
any point in the
sequence (not just at
the beginning & end)
performance
trade-off: does not
offer a random
access iterator
implemented as doubly linked list
#include <iostream>
#include <iomanip>
#include <cmath>
#include <list>
#include <string>
using namespace std;
int main (int argc, char *argv[])
{
list<string> a_list; a_list.push_back ("banana"); a_list.push_front ("apple"); a_list.push_back ("carrot"); ostream_iterator<string> out_it(cout, "\n"); copy (a_list.begin (), a_list.end (), out_it);
copy (a_list.rbegin (), a_list.rend (), out_it);
system("pause");
return 0;
}
OUTPUT: apple
banana
carrot
carrot
banana
apple
Look online for other examples....
|