the standard c++ library
DESCRIPTION
The Standard C++ Library. Main Ideas. Purpose Flexibility Efficiency Simple & Uniform Interface. Concepts. A concept is a list of properties of a type. STL defines a hierarchy of concepts for containers, iterators, and element types. Concepts for element types include: - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/1.jpg)
.
The Standard C++ Library
![Page 2: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/2.jpg)
Main Ideas
• Purpose
• Flexibility • Efficiency
• Simple & Uniform Interface
![Page 3: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/3.jpg)
Concepts•A concept is a list of properties of a type.•STL defines a hierarchy of concepts for containers, iterators, and element types. •Concepts for element types include:
•Concept B is a refinement of concept A if it imposes some additional requirements on A. (Similar to inheritance)
LessThan Comparable - types with operator< ,...
Equality Comparable - types with operator== ,...
Assignable - types with operator= and copy Ctor
![Page 4: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/4.jpg)
Main Components
AlgorithmsContainers
IteratorsFunctionObjects
Adaptors
Streams
Strings
![Page 5: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/5.jpg)
Holds copies of elements. Assumes elements have:
Copy Ctor & operator = The standard defines the interface. Two main classes
Sequential containers: list, vector,....
Associative containers:map, set ...
Containers
Assignable - types with operator= and copy Ctor
![Page 6: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/6.jpg)
Sequential Containers
Maintain a linear sequence of objects
![Page 7: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/7.jpg)
list - a linked list of objects Efficient insertion/deletion in
front/end/middle
vector - an extendable sequence of objects Efficient insertion/deletion at end Random access
deque – double-ended queue Efficient insertion/deletion at front/end Random access
Sequential Containers
![Page 8: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/8.jpg)
Containers Example - vector<T>
Array of elements of type T Random Access Can grow on as needed basis
std::vector<int> v(2); v[0]= 45; v[1]= 32; v.push_back(60);
std::vector<int> v(2); v[0]= 45; v[1]= 32; v.push_back(60);
![Page 9: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/9.jpg)
Associative Containers
Supports efficient retrieval of elements (values) based on keys.
(Typical) Implementation: red-black binary trees hash-table (SGI extension, not standard)
![Page 10: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/10.jpg)
Sorted Associative Containers
Set A set of unique keys
Map Associate a value to key (associative array) Unique value of each key
Multiset
Multimap Same, but allow multiple values
![Page 11: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/11.jpg)
Sorted Associative Containers & Order
Sorted Associative containers use operator< as default order
We can control order by using our own comparison function
To understand this issue, we need to use function object
FunctionObjects
![Page 12: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/12.jpg)
Anything that can be called as if a function.
For example:
Pointer to function
A class that implements operator()
Advantage of class: Enable accumulating information in the
function object
FunctionObjects
![Page 13: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/13.jpg)
Example
template<typename T>
class less {
public:
bool operator()(const T& lhs, const T& rhs)
{ return lhs < rhs; }
};
less<int> cmp; // declare an object
if( cmp(1,2) )
…
if( less<int>()(1,2) )
…
Creates temporary objects, and then call operator()
![Page 14: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/14.jpg)
Using Comparators
template<typename T,
typename Cmp = less<T> >
class set {
…
}
…
set<int> s1;
set<int,less<int> > s2;// same type
![Page 15: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/15.jpg)
Using Comparators
template<typename T,
typename Cmp = less<T> >
class set {
…
}
…
set<int,MyComp > s3;
MyComp cmp;
set<int,MyComp > s4(cmp);
Creates a new MyComp object.
Use given MyComp object.
![Page 16: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/16.jpg)
Iterators are allow to traverse sequences Main Methods
operator* operator++, and operator—
Different types of iterators - to support read, write and random access
Containers define their own iterator types Changing the container can invalidate the
iterator
Iterators
![Page 17: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/17.jpg)
Output IteratorInput Iterator
Trivial Iterator
Forward Iterator
Bi-directional Iterator
Random-Access Iterator
++, input ++, output
++, I/O
++, --, I/O
Pointer arithmetic
input
Iterators
![Page 18: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/18.jpg)
Iterator Types
Output: write only and can write only once Input: read many times each item Forward supports both read and write Bi-directional support also decrement Random supports random access (just like C
pointer)
Output Input Forward Bi-directional Random
Read x = *i x = *i x = *i x = *i
Write *i = x *i = x *i = x *i = x
Iteration ++ ++ ++ ++, -- ++, --, +, -, +=, -=
Comparison ==, != ==, != ==, != ==, !=, <, >, <=, >=
![Page 19: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/19.jpg)
Iterators & Containers
Bidirectional iterators: list, map, set
Random access iterators: vector
Input/output/forward iterators: iostreams
![Page 20: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/20.jpg)
Iterators and Containers
Container c
…
Container::iterator i;
for( i = c.begin(); i != c.end(); ++i)
// do something with *i
Container c
…
Container::iterator i;
for( i = c.begin(); i != c.end(); ++i)
// do something with *i
class Container {…typedef … iterator; // iterator type of containeriterator begin(); // first element of containeriterator end(); // element after last of container
![Page 21: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/21.jpg)
Iterators & Map
Suppose we work with:
map<string,int> dictionary;
map<string,int>::iterator i;
…
i = dictionary.begin();
What is the type of *i ?
![Page 22: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/22.jpg)
Iterators & Map
Every STL container type Container defines
Container::value_type
Type of elements stored in container
This is the type returned by an iteratorContainer::value_type
Container::iterator operator*();
![Page 23: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/23.jpg)
Iterators & Map
Ok, so what type of elements does a map return?
map<KeyType,ValueType> keeps pairs KeyType key – “key” of entry ValueType value – “value” of entry
![Page 24: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/24.jpg)
Pairs
template< typename T1, typename T2>
struct pair {
typedef T1 first_type;
typedef T2 first_type;
T1 first;
T2 second;
pair( const T1& x, const T2& y )
: first(x), second(y)
{}
};
![Page 25: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/25.jpg)
Map value_type
template< typename Key, typename T,
typename Cmp = less<Key> >
class map {
public:
typedef pair<const Key, T> value_type;
typedef Key key_type;
typedef T mapped_type;
typedef Cmp key_compare;
};
![Page 26: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/26.jpg)
Using map iterator
map<string,int> dict;
…
map<string,int>::iterator i;
for( i = dict.begin();
i != dict.end();
i++ )
{
cout << i->first << “ “
<< i->second << “\n”;
}
![Page 27: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/27.jpg)
Good functionality, wrong interface
For example, adaptors of basic containers
Limited interface:
stack<T,Seq>
queue<T,Seq>
Adaptors
![Page 28: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/28.jpg)
Most STL algorithms works on sequences Sequences are passed as two iterators:
beginning element element one after last
Algorithms depend on iterator type
not on container type
p q
sequence [p,q)
Algorithms
![Page 29: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/29.jpg)
Copy
template< typename In, typename Out>
Out copy(In first, In last, Out res)
{
while (first != last)
*res++ = *first++;
return res;
}
![Page 30: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/30.jpg)
template <class RandomAccessIterator, class StrictWeakOrdering> void sort(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp);
Sort
![Page 31: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/31.jpg)
Cryptic error messages
![Page 32: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/32.jpg)
Cryptic error messages
New solution in g++, define before including a
standard library header:
#define _GLIBCXX_CONCEPT_CHECKS
![Page 33: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/33.jpg)
SGI: http://www.sgi.com/tech/stl/ Dinkum: http://
www.dinkumware.com/refxcpp.html http://www.cplusplus.com/ref/
Links to documentation
![Page 34: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/34.jpg)
Main Components
AlgorithmsContainers
IteratorsFunctionObjects
Adaptors
Streams
Strings
![Page 35: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/35.jpg)
Mutableclass String { char* _data; mutable int _len; public: String(const char* data) : _data(data),_len(-1){} void capitalie() { char* p =_data; while (*p) *p= toupper(*p++); } int length() const { if (_len==-1) _len=strlen(_data); return _len; }};
![Page 36: The Standard C++ Library](https://reader036.vdocument.in/reader036/viewer/2022081514/56814ef8550346895dbc8986/html5/thumbnails/36.jpg)
istream_iterator<string> in(cin);
istream_iterator<string> endOfInput; vector<string> vect;
back_insert_iterator<vector<string> >
back(vect);
// copy input words into vector…
while( in != endOfInput )
*back++ = *in++;
// same as: vect.push_back(*in++)
Inserters