Lesson of 22 July 2009¶
Example of the use of STL: the vector container
File stl.cc
1/*
2
3 Example of use of STL (Standard Template Library)
4
5 the vector container
6
7 */
8
9#include <iostream> // for I/O
10#include <string> // string support
11#include <cmath> // for sin, cos, ...
12
13#include <vector> // the definition of STL vector container
14#include <algorithm> // the definition of the STL algorithm (sort, find,...)
15
16using namespace std ;
17
18int
19main() {
20 // define a as a vector of int
21 vector<int> a ;
22
23 a . push_back(12) ;
24 a . push_back(4) ;
25
26 cout << "a.front() = " << a.front() << '\n' ; // the first element
27 cout << "a.back() = " << a.back() << '\n' ; // the last element
28
29 cout << "size of a = " << a . size() << '\n' ;
30
31 // define b as a vector of 10 float
32 vector<float> b(10) ;
33
34 cout << "size of b = " << b . size() << '\n' ;
35
36 b[2] = 30 ; // random access to the element of b
37 b[5] = 12 ;
38 b[9] = 1.34 ;
39
40 // loop on the vector C-style way
41 for ( int i=0 ; i < b.size() ; ++i )
42 cout << "b[" << i << "] = " << b[i] << '\n' ;
43 // ^
44 // |
45 // access to the i-th element of vector b
46
47 // loop on the vector STL-style way
48 for ( vector<float>::iterator i = b . begin() ;
49 i != b . end() ;
50 ++i ) // increment the iterator to "point" to the next object in
51 // the vector b (an instance of vector<int>)
52 cout << *i << '\n' ;
53 // ^
54 // |
55 // access to the element of vector b pointed by iterator i
56
57 // Algorithms
58
59 cout << "do sort\n" ;
60
61 sort( b.begin(), b.end() ) ;
62 //reverse( b.begin(), b.end() ) ;
63 //sort( b.rbegin(), b.rend() ) ;
64
65 for ( vector<float>::iterator i = b . begin() ; i != b . end() ; ++i )
66 cout << *i << '\n' ;
67
68 vector<float>::iterator i = max_element( b.begin(), b.end() ) ;
69 cout << "maximum element = " << *i << '\n' ;
70
71 b . clear() ; // cancel all the elements of the vector
72 cout << "size of b = " << b . size() << '\n' ;
73
74 b . reserve( 21 ) ; // pre allocate memory for 21 elements
75 for ( int i=0 ; i < 21 ; ++i ) b . push_back( i ) ;
76
77 vector<float> c(30) ;
78
79 copy( b.begin(), b.end(), c.begin() ) ;
80 // ^ ^ ^
81 // +----------+ + "pointer" to the begin of the "destination" memory
82 // range to be copied
83 // it is equivalent to
84 for ( vector<float>::iterator i = b . begin(), j = c . begin() ;
85 i != b . end() ;
86 ++i, ++j )
87 *j = *i ;
88 for ( int i = 0 ; i < b.size() ; ++i ) c[i] = b[i] ;
89
90 // simulation of a matrix
91 vector<vector<float> > m ;
92
93 // to allocate for a matrix 10x20
94 m . resize(10) ; // allocate for 10 vector
95 // C-way loop
96 for ( int i = 0 ; i < m . size() ; ++i ) m[i] . resize(20) ;
97 // STL-way loop
98 for ( vector<vector<float> >::iterator i = m.begin() ;
99 i != m . end() ; ++i ) i -> resize(20) ;
100
101 m[1][3] = 123 ; // m[1] access to 2nd element of m which is a vector
102 // m[1][3] access to 3rd element of vector m[1].
103
104}
The ``dqueue`` container
File stl2.cc
1/*
2
3 Example of use of STL (Standard Template Library)
4
5 the dqueue container
6
7 */
8
9#include <iostream> // for I/O
10#include <string> // string support
11#include <cmath> // for sin, cos, ...
12
13#include <deque> // the definition of STL deque container
14#include <algorithm> // the definition of the STL algorithm (sort, find,...)
15
16using namespace std ;
17
18int
19main() {
20 // define a as a vector of int
21 deque<int> a ;
22
23 a . push_back(12) ;
24 a . push_front(4) ;
25
26 cout << "a.front() = " << a.front() << '\n' ; // the first element
27 cout << "a.back() = " << a.back() << '\n' ; // the last element
28
29 cout << "size of a = " << a . size() << '\n' ;
30
31 // define b as a vector of 10 float
32 deque<float> b(10) ;
33
34 cout << "size of b = " << b . size() << '\n' ;
35
36 b[2] = 30 ; // random access to the element of b
37 b[5] = 12 ; // implemented internally by a loop!
38 b[9] = 1.34 ;
39
40 // loop on the vector STL-style way
41 for ( deque<float>::iterator i = b . begin() ;
42 i != b . end() ;
43 ++i ) // increment the iterator to "point" to the next object in
44 // the vector b (an instance of vector<int>)
45 cout << *i << '\n' ;
46 // ^
47 // |
48 // access to the element of vector b pointed by iterator i
49
50 // Algorithms
51
52 cout << "do sort\n" ;
53
54 sort( b.begin(), b.end() ) ; // now use merge sort not quick sort
55 //reverse( b.begin(), b.end() ) ;
56 //sort( b.rbegin(), b.rend() ) ;
57
58 for ( deque<float>::iterator i = b . begin() ; i != b . end() ; ++i )
59 cout << *i << '\n' ;
60
61 deque<float>::iterator i = max_element( b.begin(), b.end() ) ;
62 cout << "maximum element = " << *i << '\n' ;
63
64 b . clear() ; // cancel all the elements of the vector
65 cout << "size of b = " << b . size() << '\n' ;
66
67 for ( int i=0 ; i < 21 ; ++i ) b . push_back( i ) ;
68 deque<float> c(30) ;
69
70 copy( b.begin(), b.end(), c.begin() ) ;
71 // ^ ^ ^
72 // +----------+ + "pointer" to the begin of the "destination" memory
73 // range to be copied
74 // it is equivalent to
75
76 for ( deque<float>::iterator i = b . begin(), j = c . begin() ;
77 i != b . end() ;
78 ++i, ++j )
79 *j = *i ;
80 for ( int i = 0 ; i < b.size() ; ++i ) c[i] = b[i] ;
81}
the set container
File stl3.cc
1/*
2
3 Example of use of STL (Standard Template Library)
4
5 the set container
6
7 */
8
9#include <iostream> // for I/O
10#include <string> // string support
11#include <cmath> // for sin, cos, ...
12
13#include <set> // the definition of STL set container
14#include <algorithm> // the definition of the STL algorithm (sort, find,...)
15
16using namespace std ;
17
18int
19main() {
20 // define a as a vector of int
21 set<int> a ;
22
23 a . insert(12) ;
24 a . insert(4) ;
25 a . insert(4) ;
26 a . insert(4) ;
27 a . insert(4) ;
28 a . insert(4) ;
29
30 cout << "size of a = " << a . size() << '\n' ;
31
32 // loop on the vector STL-style way
33 for ( set<int>::iterator i = a . begin() ;
34 i != a . end() ;
35 ++i ) // increment the iterator to "point" to the next object in
36 // the vector b (an instance of vector<int>)
37 cout << *i << '\n' ;
38 // ^
39 // |
40 // access to the element of vector b pointed by iterator i
41
42 multiset<int> b ;
43
44 b . insert(12) ;
45 b . insert(4) ;
46 b . insert(4) ;
47 b . erase(4) ;
48 b . insert(4) ;
49 b . insert(4) ;
50 b . insert(4) ;
51
52 cout << "size of b = " << b . size() << '\n' ;
53
54 // loop on the vector STL-style way
55 for ( multiset<int>::iterator i = b . begin() ;
56 i != b . end() ;
57 ++i ) // increment the iterator to "point" to the next object in
58 // the vector b (an instance of vector<int>)
59 cout << *i << '\n' ;
60 // ^
61 // |
62 // access to the element of vector b pointed by iterator i
63
64 multiset<int>::iterator i = max_element( b . begin(), b . end() ) ;
65}