From C++03 to C++11: Difference between revisions
From Gridkaschool
Jump to navigationJump to search
m (→smart pointer) |
m (→smart pointer) |
||
Line 90: | Line 90: | ||
#define BINARY_TREE_CPP |
#define BINARY_TREE_CPP |
||
#include<iostream> |
#include<iostream> |
||
#include "binary_tree.hpp" |
|||
void binary_tree::insert(node* z) { |
void binary_tree::insert(node* z) { |
Revision as of 10:07, 1 September 2014
Gridka School 2014 C++ course
- Martin Heck, KIT EKP
- Jörg Meyer, KIT SCC
Literature on C++
C++ books
- The C++ Programming Language, Bjarne Stroustrup
- Effective C++, Scott Meyers
- More Effective C++: 35 New Ways to Improve Your Programs and Designs
- Modern C++ Design, Andrei Alexandrescu
- The C++ Standard Library, Nicolai M. Josuttis
- C++ Templates, David Vanevoorde, Nicolai M. Josuttis
- Exceptional C++, Herb Sutter
- More Exceptional C++, Herb Sutter
C++11 books
- The C++ Programming Language, 4th Edition, Bjarne Stroustrup
- The C++ Standard Library: A Tutorial and Reference (2nd Edition), Nicolai M. Josuttis
- C++11 programmieren, Torsten T. Will (German)
- C++11: Der Leitfaden für Programmierer zum neuen Standard, R. Grimm (German)
C++ links
- http://www.stroustrup.com/bs_faq2.html
- http://www.cplusplus.com/reference/
- http://www.parashift.com/c++-faq/
- http://herbsutter.com/gotw/
- http://wiki.scc.kit.edu/gridkaschool/index.php/Effective_Analysis_Programming_Part_1
- http://wiki.scc.kit.edu/gridkaschool/index.php/Effective_Analysis_Programming_Part_2
C++11 links
- http://isocpp.org/get-started
- http://herbsutter.com/elements-of-modern-c-style/
- http://isocpp.org/tour
Technical aspects for the course
Gridka School slides
- Bug avoidance: The C++11 Standard offers new ways in which the compiler can help you to avoid bugs. Especially the new keywords override, final, and smart pointers.
access to machines
You can run the exercises either on your own computer or ssh to one of our prepared machines using your Gridka School account.
- connection from Linux/Unix: ssh -p24 <username>@gks-virt<xxx>.scc.kit.edu
- connection from Windows: use an ssh client like putty, or use cygwin
This is a list of hostnames: gks-virt084 gks-virt088 gks-virt098
compilaton and execution of code
Most examples consist of just one cpp file, i.e. no header file, no additional library. Use the following commands to compile and run the code:
g++ mycode.cpp ./a.out
For C++11 support do:
g++ -std=c++11 mycode.cpp ./a.out
exercises
smart pointer
- Add a memory management to the binary search tree by introducing adequate smart pointers.
binary_tree.hpp
#ifndef BINARY_TREE_HPP
#define BINARY_TREE_HPP
#include<initializer_list>
class binary_tree {
struct node {
node(int k): key(k), left(0), \
right(0), p(0) {}
int key;
node *left, right, *p;
};
public:
node* root;
void insert(node* z);
void inorder_print(node* x);
binary_tree(std::initializer_list<int> values): root(0) {
for (auto it=values.begin(); \
it!=values.end();++it)
insert(new node(*it));
}
};
#endif
binary_tree.cpp
#define BINARY_TREE_CPP
#include<iostream>
#include "binary_tree.hpp"
void binary_tree::insert(node* z) {
node* y=0;
node* x=root;
while (x) {
y=x;
if (z->key < x->key)
x=x->left;
else
x=x->right;
}
z->p = y;
if (!y)
root=z; // tree was empty
else if (z->key < y->key)
y->left = z;
else
y->right = z;
}
void binary_tree::inorder_print(node* x) {
if (x) {
inorder_print(x->left);
std::cout<<x->key<<" ";
inorder_print(x->right);
}
}
int main() {
binary_tree bt{12,5,5,7,2,4};
bt.inorder_print(bt.root);//2 4 5 5 7 12
}