stl !!!generic programming!!!

27
STL !!!generic programming!!! Anar Manafov ([email protected])

Upload: ohio

Post on 20-Jan-2016

52 views

Category:

Documents


0 download

DESCRIPTION

STL !!!generic programming!!!. Anar Manafov ([email protected]). STL ( S tandard T emplate L ibrary) or STL stands for St epanov and L ee. The Evolution STL is not a new library as compared to other libraries. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: STL  !!!generic programming!!!

STL !!!generic programming!!!

Anar Manafov ([email protected])

Page 2: STL  !!!generic programming!!!

STL (Standard Template Library)or STL stands for Stepanov and Lee

The EvolutionSTL is not a new library as compared to other libraries. Originally, the development of the STL was started by Alexander Stepanov at HP in 1979. Later, he was joined by David Musser and Meng Lee. In 1994, STL was included into ANSI and ISO C++.

recommended link (Al Stevens Interviews Alex Stepanov, March 1995 issue of Dr. Dobb's Journal, ): http://www.sgi.com/tech/stl/drdobbs-interview.html

http://www.stepanovpapers.com/

Page 3: STL  !!!generic programming!!!

STL Components

Here is a list of elements of the STL. The first three of them are fundamental items.

- Container (An object that holds other objects)- Algorithm (A function that acts on containers)- Iterator (A pointer-like object)

- Allocator (This item manages memory allocation in a container)- Adaptor (Transforms one object into another)- Predicate (A function that returns a boolean value, true or false.)- Function Object (A class that defines operator().)

Recommended link: http://www.sgi.com/tech/stl/ and http://www.sgi.com/tech/stl/stl_index.html

Page 4: STL  !!!generic programming!!!

STL Components

Page 5: STL  !!!generic programming!!!

containers, iterators, algorithms

vector <T>

list <T>

sort

find

Page 6: STL  !!!generic programming!!!

containers, iterators, algorithms#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

int ia[10]={33,17,11,88,43,99,6,9,12,7};

int main(){

vector<int> x(ia,ia+10);

sort(x.begin(), x.end());

9 11 12 17 337 43 88 996

11 88 43 99 617 9 12 733

begin()end()

Page 7: STL  !!!generic programming!!!

// grab value to search for

int s_value;

cin >> s_value;

// search for an element

vector<int>::iterator found;

found=find(x.begin(),x.end(),s_value);

if(found != x.end()) {

cout << "search value found!\n";

}

else {

cout << "search value not found!\n";

}

}

Page 8: STL  !!!generic programming!!!

list instead of vector

#include <list>

#include <algorithm>

#include <iostream>

using namespace std;

int ia[10]={33,17,11,88,43,99,6,9,12,7};

int main(){

list<int> x(ia,ia+10);

sort(x.begin(), x.end());

Page 9: STL  !!!generic programming!!!

// grab value to search for int s_value; cin >> s_value;

// search for an element list<int>::iterator found; found=find(x.begin(),x.end(),s_value); if(found != x.end()) { cout << "search value found!\n"; } else { cout << "search value not found!\n"; }}

Page 10: STL  !!!generic programming!!!

Algorithms with built-in arrays

#include <algorithm>

#include <iostream>

using namespace std;

int ia[10]={33,17,11,88,43,99,6,9,12,7};

int main(){

int s_value;

cout << "enter search value: ";

cin >> s_value;

int *found;

found=find(&ia[0],&ia[10],s_value);

if(found != ia+10) ...

Page 11: STL  !!!generic programming!!!

Containers

Sequence ContainersVector

List

Deque

Stack

Queue Adaptor

Priority_queue

Almost ContainersBuilt-in arrays, strings, valarrays, bitsets

Associative ContainersMap

Multimap

Set

Multiset

Page 12: STL  !!!generic programming!!!

Containers

Page 13: STL  !!!generic programming!!!

ContainerVector: Deque:

List:

Set/Multiset: Map/Multimap:

Page 14: STL  !!!generic programming!!!

string Type#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

string s1("Hello");

string s2("World");

cout << s1 + " " + s2 + '\n';

string s3(s1 + " " + s2);

cout << '\"'<< s3 << "\" has " << s3.length()

<< " characters" << endl;

replace(s3.begin(), s3.end(), 'W', 'w');

cout << s3 << endl;

Page 15: STL  !!!generic programming!!!

Constructors

container()

container(n) (not for ass. cont.)

container(n,x) (not for ass. cont.)

container(first,last)

container(c)

~container()

Page 16: STL  !!!generic programming!!!

Stack, Queue, List Operations

push_back() pop_back()

push_front() pop_front()

insert(p,x)

insert(p,n,x)

insert(p,first,last)

erase(p)

erase(first,last)

clear()

Page 17: STL  !!!generic programming!!!

Algorithms

• copy• sort• find• fill• partition• insert, delete• union, intersection• accumulate• ...

Page 18: STL  !!!generic programming!!!

Iterators

• The glue that makes it possible to use generic algorithms and orthogonalize those algorithms from the data structures

• An iterator i is a generalized means of traversing a data structure:– for an array, an array index or a pointer

• Dereferencing an iterator, *i, is guaranteed to give the item, but an iterator does not obey all pointer operations

Page 19: STL  !!!generic programming!!!

Iterators, Element Access

begin()

end()

rbegin()

rend()

front()

back()

[]

at()

Points to first element

Points to one-past-last element

Points to first element of reverse seq.

Points to one-past-last element of rev. seq.

First element

Last element

Subscripting, unchecked access

Subscripting, checked access

Page 20: STL  !!!generic programming!!!

Iterator Operationsoutput input forward bidirectional random-access

Read =*p =*p =*p =*p

Access -> -> -> -> []

Write *p= *p= *p= *p=

Iteration ++ ++ ++ ++ -- ++ -- + -

Comparison == != == != == != == != < >

Page 21: STL  !!!generic programming!!!

Iterators (continued)

container<T>::iterator first=c.begin();

container<T>::iterator last=c.end();

[first,last)

++i;

first == last;

i != last;

i + n; (long jump)

*i = x;

x = *i;

Page 22: STL  !!!generic programming!!!

Nonmodifying Sequence Operations

for_each()

find()

find_if()

find_first_of()

adjacent_find()

count()

count_if()

mismatch()

equal()

search()

find_end()

search_n()

list<Person*> all;...for_each(all.begin(),all.end(),Print(cout));

Page 23: STL  !!!generic programming!!!

Function objects

class bThan{

public:

bThan(int x): testVal(x) {}

const int testVal;

bool operator()(int val){return val>testVal;}

};

list<int>::iterator firstBig =

find_if(aList.begin(),aList.end(),bThan(12));

Page 24: STL  !!!generic programming!!!

OO ?

• STL is not OO, is OOP dead?

• Not all problems are best solved in OOP fashion!

• Many problems are best solved in an OOP manner.

• Know as much as you can about as many styles of programming as you can, an use the style most appropriate to the problem.

Page 25: STL  !!!generic programming!!!

#include <iostream>#include <map>#include <string>

using namespace std;

int main(){

map<string, long, less<string> > directory;

directory["Anar"] = 2128;directory["Victor"] = 1395;directory["Demo"] = 12344;

string name;while (cin >> name){

if ( directory.find(name) != directory.end() )cout << "The phone number for " << name << " is " << directory[name] << endl;

elsecout << "Sorry, no listing for " << name << endl;

}}

Page 26: STL  !!!generic programming!!!

#include <iostream>#include <map>#include <string>

using namespace std;

typedef map<string, long, less<string> > Directory_t;typedef Directory_t::value_type Entry;

int main(){

Directory_t directory;// Will change the value of the key or insert if the key is exist alreadydirectory["Anar"] = 2128;// will insert or fail if the key is exist alreadydirectory.insert ( make_pair<string, long>("Victor", 1395) );directory.insert( Entry("Demo", 12344) );

string name;while (cin >> name){

Directory_t::iterator iter = directory.find(name);if ( iter != directory.end() )

cout << "The phone number for " << iter->first << " is " << (*iter).second << endl;else

cout << "Sorry, no listing for " << name << endl;}

}

Page 27: STL  !!!generic programming!!!

/*! \fn void TrimRight(std::basic_string<_T> &_str, const std::basic_string<_T> &_Val)\brief Trims trailing characters from the string.\param _Val - [in] The target characters to be trimmed.

*/template<typename _T>void TrimRight(std::basic_string<_T> &_str, const std::basic_string<_T> &_Val){

_str.resize( _str.find_last_not_of( _Val ) + 1 );}

/*! \fn void TrimLeft(std::basic_string<_T> &_str, const std::basic_string<_T> &_Val)\brief Trims leading characters from the string.\param _Val - [in] The target characters to be trimmed.

*/template<typename _T>void TrimLeft(std::basic_string<_T> &_str, const std::basic_string<_T> &_Val){

_str.erase( 0, _str.find_first_not_of( _Val ) );}

Some real life examplessmall, but very handy!