Overview, C++ lecture 4
Topics for today:
●
Standard Template Library
Example from STL: vector
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> vNumbers; vNumbers.push_back(1); cout << vNumbers[0]; return 0;
vector
●
Similar to an ordinary C array, but dynamic, i.e.
the length can both increase and decrease.
Commonly used vector
functions
●
[] - get/set, with bound checking in MSVC
●
at - get/set, with bound checking
●
push_back - appending objects
●
capacity - the allocated nr of elem.
●
reserve - request a capacity change
●
clear - clear the content
●
front - the first element
Standard Template Library
(STL)
●
Originally created by Silicon Graphics
●
Became a part of the C++ in 1994
●
Many different implementations appeared
since then.
Common STL templates
Sequence containers Function
vector Dynamic array, one open end deque Dynamic array, two open ends list Doubly linked list
Container adaptors Function
stack LIFO (Last In - First Out) queue FIFO (First In – First Out) priority_queue Priority queue
Associative containers Function
set Mathematical set
STL collection functions
Function Returns
empty True if empty
size Number of elements
begin Forward iterator set to the first element
end Forward iterator set just past the last element rbegin Backward iterator set to the last element
rend Backward iterator set just before the first element clear Erases all elements, by calling all destructors and
setting the size tozero.
Queue example
#include <queue> queue<int> myQueue; myQueue.push(1); myQueue.push(7); myQueue.push(4);
while (!myQueue.empty()) {
cout << myQueue.front() << endl; myQueue.pop(); } ●
Output:
1 7 4Priority queue example
#include <queue>
priority_queue<Task> prioQueue;
prioQueue.push(Task(4, "Go to the cinema"));
prioQueue.push(Task(9, "Solve the programming assignments"));
while (!prioQueue.empty()) {
cout << prioQueue.top().task << endl; prioQueue.pop();
}
●
Output:
Solve the programming assignments Go to the cinema
Task class definition
class Task {
public:
Task(int _priority, string _task); bool operator<(const Task&) const; int priority;
string task; };
// The constructor
Task::Task(int _priority, string _task) : priority(_priority),
task(_task) {
};
// The comparison function
bool Task::operator<(const Task& right) const {
return priority < right.priority; }
set example
#include <set> set <int> mySet; mySet.insert(4); mySet.insert(2); mySet.insert(4);
for ( set<int>::iterator it = mySet.begin(); it != mySet.end();
it++ ) {
cout << *it << endl; }
// With BOOST_FOREACH defined as foreach, // the loop header could have been written: // foreach(int& val, mySet)
●
Output:
map
#include <map>
map <int, double> myMap; myMap[1] = 4.7;
myMap[3] = 8.5; myMap[4] = 9.0;
// Accessing the data using a key
cout << "myMap[4] = " << myMap[4] << endl;
// Traversing all elements
for ( map<int,double>::iterator it = myMap.begin(); it != myMap.end();
it++) {
cout << it->first << " " << it->second << endl; }
// BOOST_FOREACH runs into trouble here
●
Output:
myMap[4] = 9 1 4.7
3 8.5 4 9
Looping over a vector
using namespace std;
// Create a vector with two elements
vector<int> vec; vec.push_back(1); vec.push_back(2);
// Print the vector using an iterator
vector<int>::iterator it; for( it = vec.begin(); it != vec.end(); it++ ) { cout << *it << ” ”; }
Loop in latest C++ version
vector<int> vec;
vec.push_back( 10 ); vec.push_back( 20 ); for (int i : vec ) {
cout << i; }
Traversing using index
variable
using namespace std;
// Create a vector with two elements
vector<int> vIntegers; vIntegers.push_back(1); vIntegers.push_back(2);
for(int i=0; i<myIntVector.size(); i++)
{
cout << vIntegers[i] << " "; }
Sequence operations
vector<int> v1; vector<int> v2;
// Create some elements
for (int i=0; i<10; i++) v1.push_back(i);
random_shuffle(v1.begin(), v1.end()); v2 = v1;
sort(v2.begin(), v2.end());
// Print the vectors
// Assuming that BOOST_FOREACH is available and remapped to foreach
foreach(int val, v1) cout << val << " "; cout << endl; foreach(int val, v2) cout << val << " "; ●
Example output:
8 1 9 2 0 5 7 3 4 6 0 1 2 3 4 5 6 7 8 9Connecting Python and C
●
Using C-functions from Python: Wrapper
functions in C must be created
●
Boost.Python or SWIG simplifies this process.
●
Help on using SWIG:
Source file
//func.cpp
// Just contains a simple test function
int add(int a, int b) {
return a+b; }
SWIG interface file
//func.i
%module func %{
extern int add(int, int); %}
Compiling for use in Python
● Compile func.c and generate the object file func.o:$ gcc -c -fpic func.c
● Create the wrapper code from the specification i func.i. Generates
func_wrap.c and func.py: $ swig -python func.i
● Compile the wrapper:
$ gcc -c -fpic func_wrap.c -I/usr/include/python2.4
● Link the object files to _func.so:
Use in Python
rontok> python
Python 2.3.5 (#2, Feb 23 2005, 18:16:39) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import func >>> func.add(2,7) 9
SWIG in Windows
●
Swigwin together with Microsoft Visual C++.
●
http://www.swig.org/download.html
●
Look at the examples for Python which comes
STL material
●
http://en.wikipedia.org/wiki/Standard_Template_Library
●