This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
int ja[] = {0,1,2,3,4,5,6}; int ja[] = {0,1,2,3,4,5,6}; s = array_size(ja);s = array_size(ja);cout << "function template approach: " << s << endl; cout << "function template approach: " << s << endl; //correct, returns 7//correct, returns 7
// int* pa = ja; int* pa = &ja[0]; s = array_size(pa); cout << "macro_array_size = " << s << endl; // fails, no matching function for call // fails, no matching function for call // to `array_size(int*&) // to `array_size(int*&)
CalcArraySize<int, <int, 1010>> a(ia); s = a.getSize();
10 will be a fixed limit and that’s not exactly what we want.
Array SizeArray SizeTwo arguments are still requiredTwo arguments are still required
1919
This works, but with two arguments required.
Vector(T* a, unsigned s) : size(s) { Vector(T* a, unsigned s) : size(s) { data = new T[size];data = new T[size]; for(int i=0; i < size; i++) {for(int i=0; i < size; i++) {
STLSTL is a set of general-purpose template classes, built using the template mechanism.
What are its main parts?What are its main parts?
• Containers• Iterators• Algorithms
8
STLSTLContainersContainers
1919
ContainersContainers are objects that hold other objects.
Sequence container: Sequence container: A container whose elements are kept in an ordinal sequence, like an array. The position of each element is independent of its value.
• vector (direct array access)• deque (double-ended queue)• list (linked-list, no indexed access, faster insertion/deletion)
What are they?What are they?
Different types of containers:Different types of containers:
9
STLSTLContainersContainers
1919
Associative container:Associative container:
A container whose elements are kept in a sorted order for allowing efficient retrieval of values based on keys. The positions of the elements are completely determined by their values and those of the other elements in the container.
• map (look-up table structure)• sets (represent mathematical sets using union and intersection operations)
10
STLSTLPreamble to IteratorsPreamble to Iterators
1919
#include <iostream>using namespace std;
template< class T> // a generic function to print an arrayvoid printArrayprintArray( T* a, int len ){ for(int j=0; j < len; j++) { cout << a[j] << " "; } cout << endl;}
Function Template
11
STLSTLPreamble to IteratorsPreamble to Iterators
1919
int main(){
//typedef long int MyType; typedef long long int MyType;
const int Length = 10; MyType array[Length];
cout << "Size of MyType is: " << sizeof(MyType ) << endl; for( int i=0;i<Length;i++) { array[i] = i + 1; } printArray( array, Length );
This code will work regardless of what MyTypeMyType actually is.
12
#include <iostream>using namespace std;
template< class T> // a generic func to print an arrayvoid printArray( T * a, int len ){ for(int j=0;j<len;j++) cout << a[j] << " "; cout << endl;}
int main(){
//typedef long int MyType; typedef long long int MyType;
const int Length = 10; MyType arrayarray[Length];
cout << "Size of MyType is: " << sizeof(MyType ) << endl;
for( int i=0;i<Length;i++){ arrayarray[i] = i + 1; }
// access via iterator vector<char>::iterator p = v.begin(); while(p != v.end()) { cout << *p*p << " "; ++p; }
Declaring an iterator:container_name :: iterator iterator_name
25
Iterator
• An Iterator is just a convenient pseudo-pointer that is set up as a type to use associated with each container class.
• Various operators will have been set up to use with them e.g. =, ==, != and +=
• Standard idiomatic form:
int array[10];
for(int i=0; i<10; i++){
}
• Becomes:
vector<int> v(10);
vector<int>::iterator it;
for(it=v.begin();it != v.end();it++){
}
26
IteratorsIterators
IteratorsIterators are objectsobjects designed to give us the ability to cycle through the content of a container. They act like pointers, locating one item in the container at a time.
All iterators have the same basic functionality, regardless of the type of container to which they are attached. The fundamental operations are:
• initialise the iterator at some initial position in the container
• return the data value stored at the current position
• change the data value stored at the current position
• determine whether there actually is an item at the iterator’s current position
• advance to the next position in the container
Iterators can be adapted to provide backward traversal.
template <class ForwIterForwIter>void printprint(ForwIterForwIter first, ForwIterForwIter last, const char* title){ cout << title << endl; while ( first != last) cout << *first++ << '\t'; cout << endl;}
IteratorsIterators
28
int main(){
int data[3] = { 9, 10, 11};
vector<int> d(data, data + 3); //auxiliary constructor
vector<int>::reverse_iterator p = d.rbegin();
print(p, d.rend(), "Reverse"); //...}
Example that uses a reverse iterator to traverse a sequence.
IteratorsIterators
See vector_2009.cpp
See vector_countries.cpp
29
Let’s have a look at the Matrix class template implemented by connecting to the STL vector via composition.
SamplesSamples
2-D array using pure vector<vector<int>*>
See vector_of_vectors.cpp
See Matrix_stl.cpp
30
• The standard template library (STLSTL) is the C++ library that provides generic programming for many standard data structures and algorithms.
• ContainersContainers come in two major families: sequence (are ordered) and associativeassociative (have keys for looking up elements).
• Iterators can be thought of as an enhanced pointer type.
• The algorithms use iterators to access containers.
SummarySummary
31
Other standard components
STL relies upon several other standard components for support:
allocators: to manage memory allocation for a container
predicates: special functions returning true/false results
comparison functions, etc.
32
C++ StandardC++Standard: http://www.cygnus.com/misc/wp/
International standard for the C++ programming language approved!
Morristown, New Jersey, USA, Friday, November 14, 1997
FOR IMMEDIATE RELEASE
This week, technical experts representing eight countries and about 40 companies involved with software technologies met in Morristown, New Jersey and completed the content of an international standard for the C++ programming language.
33
Scope of the Standard:
The C++ standard covers both the C++ language itself and its standard library.
The standard library provides
•standard input/output,
•strings,
•containers (such as vectors, lists, and strings),
•non-numerical algorithms (such as sort, search, and merge), and support for numeric computation.
34
The ISO/ANSI Standard for C++ was unanimously approved by the C++ Standardization Committee on June 23, 1998.
The current status (June’99) is that C++ has an International Standard and the committee are in the process of handling Defect Reports. We shall continue to do this until 2003 when ISO rules require us to revise the Standard.