• No results found

Overview, C++ lecture 4. Topics for today: Standard Template Library Connecting Python and C++

N/A
N/A
Protected

Academic year: 2021

Share "Overview, C++ lecture 4. Topics for today: Standard Template Library Connecting Python and C++"

Copied!
23
0
0

Loading.... (view fulltext now)

Full text

(1)

Overview, C++ lecture 4

Topics for today:

Standard Template Library

(2)

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;

(3)

vector

Similar to an ordinary C array, but dynamic, i.e.

the length can both increase and decrease.

(4)

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

(5)

Standard Template Library

(STL)

Originally created by Silicon Graphics

Became a part of the C++ in 1994

Many different implementations appeared

since then.

(6)

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

(7)

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.

(8)

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 4

(9)

Priority 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

(10)

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; }

(11)

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:

(12)

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

(13)

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 << ” ”; }

(14)

Loop in latest C++ version

vector<int> vec;

vec.push_back( 10 ); vec.push_back( 20 ); for (int i : vec ) {

cout << i; }

(15)

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] << " "; }

(16)

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 9

(17)

Connecting 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:

(18)

Source file

//func.cpp

// Just contains a simple test function

int add(int a, int b) {

return a+b; }

(19)

SWIG interface file

//func.i

%module func %{

extern int add(int, int); %}

(20)

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:

(21)

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

(22)

SWIG in Windows

Swigwin together with Microsoft Visual C++.

http://www.swig.org/download.html

Look at the examples for Python which comes

(23)

STL material

http://en.wikipedia.org/wiki/Standard_Template_Library

http://www.cplusplus.com/reference/stl/

References

Related documents

sort() method in list class was written in C, not Python And is present at run-time in machine language. All of the Python standard functions and classes were written in C That’s

 Despite the significant problems of the industry NOT all hedge funds are the same − Dispersion of returns across hedge fund strategies has been extremely wide. − Even more

We use extensive simulations to show that our improved XCP (iXCP) is able to achieve nearly 100% link utilization and max-min fairness in steady state. We use a theoretical model

• Facilitates the gluing of C/C++ libs to Python, allowing you to partition the application to the languages that can best handle each kind of task, building on strengths instead of

Smithsonian Institution Archives, Accession 92-138, Smithsonian Institution, Office of Design and Construction, Project Files.

At an European level the implementation of CDM , through national or local application profiles would enable European Universities to describe and publish their curricula and

Despite a reduction in its powers, the King is still granted an absolutely unique position in the political system and keeps maintaining extremely

Remodeling of TRPV1 and T-type channels during PDN development All in all, in rats of the same age and duration of diabetes in the tran- sition period of PDN development we