Presentation is loading. Please wait.

Presentation is loading. Please wait.

STL !!!generic programming!!! Anar Manafov

Similar presentations


Presentation on theme: "STL !!!generic programming!!! Anar Manafov"— Presentation transcript:

1 STL !!!generic programming!!! Anar Manafov (A.Manafov@gsi.de)

2 STL (Standard Template Library) or STL stands for Stepanov and Lee The Evolution STL 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/

3 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.htmlhttp://www.sgi.com/tech/stl/

4 STL Components

5 containers, iterators, algorithms vector list sort find

6 containers, iterators, algorithms #include using namespace std; int ia[10]={33,17,11,88,43,99,6,9,12,7}; int main(){ vector x(ia,ia+10); sort(x.begin(), x.end()); 911121733 7 43 88 996 118843996 17 9 12 733 begin() end()

7 // grab value to search for int s_value; cin >> s_value; // search for an element vector ::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"; }

8 list instead of vector #include using namespace std; int ia[10]={33,17,11,88,43,99,6,9,12,7}; int main(){ list x(ia,ia+10); sort(x.begin(), x.end());

9 // grab value to search for int s_value; cin >> s_value; // search for an element list ::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"; }

10 Algorithms with built-in arrays #include 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)...

11 Containers Sequence Containers Vector List Deque Stack Queue Adaptor Priority_queue Almost Containers Built-in arrays, strings, valarrays, bitsets Associative Containers Map Multimap Set Multiset

12 Containers

13 Container Vector: Deque: List: Set/Multiset:Map/Multimap:

14 string Type #include 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;

15 Constructors container() container(n) (not for ass. cont.) container(n,x) (not for ass. cont.) container(first,last) container(c) ~container()

16 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()

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

18 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

19 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

20 Iterator Operations

21 Iterators (continued) container ::iterator first=c.begin(); container ::iterator last=c.end(); [first,last) ++i; first == last; i != last; i + n; (long jump) *i = x; x = *i;

22 Nonmodifying Sequence Operations for_each() find() find_if() find_first_of() adjacent_find() count() count_if() mismatch() equal() search() find_end() search_n() list all;... for_each(all.begin(),all.end(),Print(cout));

23 Function objects class bThan{ public: bThan(int x): testVal(x) {} const int testVal; bool operator()(int val){return val>testVal;} }; list ::iterator firstBig = find_if(aList.begin(),aList.end(),bThan(12));

24 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.

25 #include using namespace std; int main() { map > 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; else cout << "Sorry, no listing for " << name << endl; }

26 #include using namespace std; typedef map > 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 already directory["Anar"] = 2128; // will insert or fail if the key is exist already directory.insert ( make_pair ("Victor", 1395) ); directory.insert( Entry("Demo", 12344) ); string name; while (cin >> name) { Directory_t::iterator iter = directory.find(name); if ( iter != directory.end() ) cout first << " is " << (*iter).second << endl; else cout << "Sorry, no listing for " << name << endl; }

27 /*! \fn void TrimRight(std::basic_string &_str, const std::basic_string &_Val) \brief Trims trailing characters from the string. \param _Val - [in] The target characters to be trimmed. */ template void TrimRight(std::basic_string &_str, const std::basic_string &_Val) { _str.resize( _str.find_last_not_of( _Val ) + 1 ); } /*! \fn void TrimLeft(std::basic_string &_str, const std::basic_string &_Val) \brief Trims leading characters from the string. \param _Val - [in] The target characters to be trimmed. */ template void TrimLeft(std::basic_string &_str, const std::basic_string &_Val) { _str.erase( 0, _str.find_first_not_of( _Val ) ); } Some real life examples small, but very handy!


Download ppt "STL !!!generic programming!!! Anar Manafov"

Similar presentations


Ads by Google