Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi and Cyrill Stachniss

Similar documents
CS302 - Data Structures using C++

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Fundamentals of Programming CS-110. Lecture 2

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi and Cyrill Stachniss

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

The C++ Language. Arizona State University 1

CSCI 1061U Programming Workshop 2. C++ Basics

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Computer Programming : C++

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Introduction to Programming EC-105. Lecture 2

VARIABLES & ASSIGNMENTS

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Input And Output of C++

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Why Is Repetition Needed?

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Getting started with C++ (Part 2)

Types, Values, Variables & Assignment. EECS 211 Winter 2018

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

CSCI 123 Introduction to Programming Concepts in C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

This watermark does not appear in the registered version - Slide 1

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

1 Unit 8 'for' Loops

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CS242 COMPUTER PROGRAMMING

Programming with C++ as a Second Language

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Chapter 7. Additional Control Structures

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

LECTURE 02 INTRODUCTION TO C++

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

A First Program - Greeting.cpp

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Programming. C++ Basics

Objectives. In this chapter, you will:

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Creating a C++ Program

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CS2141 Software Development using C/C++ C++ Basics

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

BITG 1233: Introduction to C++

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

CMPS 221 Sample Final

C++ Programming: From Problem Analysis to Program Design, Third Edition

Exceptions, Case Study-Exception handling in C++.

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Lecture 7. Log into Linux New documents posted to course webpage

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Chapter 2 Basic Elements of C++

Increment and the While. Class 15

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

2 nd Week Lecture Notes

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Chapter 1 INTRODUCTION

REVIEW. The C++ Programming Language. CS 151 Review #2

CSCE 110 PROGRAMMING FUNDAMENTALS

Introduction to Programming using C++

Introduction to Programming

From Pseudcode Algorithms directly to C++ programs

Your First C++ Program. September 1, 2010

Professor Peter Cheung EEE, Imperial College

Introduction to Scientific Programming with C++

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lecture 3 Tao Wang 1

CSI33 Data Structures

Chapter 2: Introduction to C++

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Variables. Data Types.

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Name SECTION: 12:45 2:20. True or False (12 Points)

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Transcription:

Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi and Cyrill Stachniss

Outline Intro to C++ Variables and basic types Built-in types Strings Vector and array Control structures If statement Switch statement Loops Git and homework submission 2

Declaring variables Variable declaration always follows pattern: <TYPE> <NAME> [ = <VALUE>]; Every variable has a type Variables cannot change their type Always initialize variables if you can 1 int sad_uninitialized_var; 2 bool initializing_is_good = true; 3

Naming variables Name must start with a letter Give variables meaningful names Don t be afraid to use longer names Don t include type in the name Don t use negation in the name GOOGLE-STYLE name variables in snake_case all lowercase, underscores separate words C++ is case sensitive: some_var is different from some_var 0 Google naming rules: https://google.github.io/styleguide/cppguide.html#general_naming_rules 4

Built-in types Out of the box types in C++: 1 bool this_is_fun = false; // Boolean: true or false. 2 char carret_return = '\n'; // Single character. 3 int meaning_of_life = 42; // Integer number. 4 short smaller_int = 42; // Short number. 5 long bigger_int = 42; // Long number. 6 float fraction = 0.01f; // Single precision float. 7 double precise_num = 0.01; // Double precision float. 8 auto some_int = 13; // Automatic type [ int]. 9 auto some_float = 13.0f; // Automatic type [ float]. 10 auto some_double = 13.0; // Automatic type [ double]. [Advanced] If curious read detailed info here: http://en.cppreference.com/w/cpp/language/types 5

Operations on arithmetic types All character, integer and floating point types are arithmetic Arithmetic operations: +, -, *, / Comparisons <, >, <=, >=, == return bool a += 1 a = a + 1, same for -=, *=, /=, etc. Avoid == for floating point types 6

Some additional operations Boolean variables have logical operations or:, and: &&, not:! 1 bool is_happy = (! is_hungry && is_warm) is_rich Additional operations on integer variables: / is integer division: i.e. 7 / 3 == 2 % is modulo division: i.e. 7 / 3 == 1 Increment operator: a++ ++a a += 1 Decrement operator: a-- --a a -= 1 Do not use de- increment operators within another expression, i.e. a = (a++) + ++b Coding Horror image from Code Complete 2 book by Steve McConnell 7

Strings #include <string> to use std::string Concatenate strings with + Check if str is empty with str.empty() Works out of the box with I/O streams 1 # include <iostream > 2 # include <string > 3 int main() { 4 std:: string hello = " Hello"; 5 std:: cout << " Type your name:" << std:: endl; 6 std:: string name = ""; // Init empty. 7 std:: cin >> name; // Read name. 8 std:: cout << hello + ", " + name + "!" << std:: endl; 9 return 0; 10 } 8

Use std::array for fixed size collections of items #include <array> to use std::array Store a collection of items of same type Create from data: array<float, 3> arr = {1.0f, 2.0f, 3.0f}; Access items with arr[i] indexing starts with 0 Number of stored items: arr.size() Useful access aliases: First item: arr.front() == arr[0] Last item: arr.back() == arr[arr.size() - 1] 9

Use std::vector when number of items is unknown before-wise #include <vector> to use std::vector Vector is implemented as a dynamic table Access stored items just like in std::array Remove all elements: vec.clear() Add a new item in one of two ways: vec.emplace_back(value) [preferred, c++11] vec.push_back(value) [historically better known] Use it! It is fast and flexible! Consider it to be a default container to store collections of items of any same type 10

Optimize vector resizing Many push_back/emplace_back operations force vector to change its size many times reserve(n) ensures that the vector has enough memory to store n items The parameter n can even be approximate This is a very important optimization 1 std::vector <std::string > vec; 2 const int kiternum = 100; 3 // Always call reserve when you know the size. 4 vec. reserve( kiternum); 5 for ( int i = 0; i < kiternum; ++i) { 6 vec. emplace_back(" hello"); 7 } 11

Example vector 1 # include <string > 2 # include <vector > 3 # include <iostream > 4 using namespace std; 5 int main() { 6 vector <int > numbers = {1, 2, 3}; 7 vector <string > names = {" Igor", " Cyrill"}; 8 names. push_back(" another_string"); 9 cout << " First name: " << names. front() << endl; 10 cout << " Last number: " << numbers. back() << endl; 11 return 0; 12 } 12

Variables live in scopes There is a single global scope Local scopes start with { and ends with } All variables belong to the scope where they have been declared All variables die in the end of their scope This is the core of C++ memory system 1 int main() { // Start of main scope. 2 float some_float = 13.13f; // Create variable. 3 { // New inner scope. 4 auto another_float = some_float; // Copy variable. 5 } // another_float dies. 6 return 0; 7 } // some_float dies. 13

Any variable can be const Use const to declare a constant The compiler will guard it from any changes Keyword const can be used with any type GOOGLE-STYLE name constants in CamelCase starting with a small letter k: const float kimportantfloat = 20.0f; const int ksomeint = 20; const std::string khello = "hello"; const is part of type: variable ksomeint has type const int Tip: declare everything const unless it must be changed 14

References to variables We can create a reference to any variable Use & to state that a variable is a reference float& ref = original_variable; std::string& hello_ref = hello; Reference is part of type: variable ref has type float& Whatever happens to a reference happens to the variable and vice versa Yields performance gain as references avoid copying data 15

Const with references References are fast but reduce control To avoid unwanted changes use const const float& ref = original_variable; const std::string& hello_ref = hello; 1 # include <iostream > 2 using namespace std; 3 int main() { 4 int num = 42; // Name has to fit on slides 5 int& ref = num; 6 const int& kref = num; 7 ref = 0; 8 cout << ref << " " << num << " " << kref << endl; 9 num = 42; 10 cout << ref << " " << num << " " << kref << endl; 11 return 0; 12 } 16

If statement 1 if (STATEMENT) { 2 // This is executed if STATEMENT == true 3 } else if ( OTHER_STATEMENT) { 4 // This is executed if: 5 // ( STATEMENT == false) && ( OTHER_STATEMENT == true) 6 } else { 7 // This is executed if neither is true 8 } Used to conditionally execute code All the else cases can be omitted if needed STATEMENT can be any boolean expression 17

Switch statement 1 switch( STATEMENT) { 2 case CONST_1: 3 // This runs if STATEMENT == CONST_1. 4 break; 5 case CONST_2: 6 // This runs if STATEMENT == CONST_2. 7 break; 8 default: 9 // This runs if no other options worked. 10 } Used to conditionally execute code Can have many case statements break exits the switch block STATEMENT usually returns int or enum value 18

While loop 1 while ( STATEMENT) { 2 // Loop while STATEMENT == true. 3 } Example while loop: 1 bool condition = true; 2 while ( condition) { 3 condition = /* Magically update condition. */ 4 } Usually used when the exact number of iterations is unknown before-wise Easy to form an endless loop by mistake 19

For loop 1 for ( INITIAL_CONDITION; END_CONDITION; INCREMENT) { 2 // This happens until END_CONDITION == false 3 } Example for loop: 1 for ( int i = 0; i < COUNT; ++i) { 2 // This happens COUNT times. 3 } In C++ for loops are very fast. Use them! Less flexible than while but less error-prone Use for when number of iterations is fixed and while otherwise 20

Range for loop Iterating over a standard containers like array or vector has simpler syntax Avoid mistakes with indices Show intent with the syntax Has been added in C++11 1 for ( const auto& value : container) { 2 // This happens for each value in the container. 3 } 21

Exit loops and iterations We have control over loop iterations Use break to exit the loop Use continue to skip to next iteration 1 while (true) { 2 int i = /* Magically get new int. */ 3 if (i % 2 == 0) { 4 cerr << i << endl; 5 } else { 6 break; 7 } 8 } 22

Free software for distributed version control synchronizes local and remote files stores a history of all changes 23

What is synchronized? Local files on a computer Remote Files in the repository We are using a Gitlab server Example repository: https://gitlab.igg.uni-bonn.de/teaching/cpp-homeworks-2018 24

Typical workflow Cloning a repository: git clone <repo_url> <local_folder> In <local_folder>: Change files git add <files> git commit -am 'descriptive message' git push origin master Git the simple guide: http://rogerdudler.github.io/git-guide/ 25

Submit homeworks through Git Log in to https://gitlab.igg.uni-bonn.de/ Request access to cpp-2018 group: https://gitlab.igg.uni-bonn.de/students/cpp-2018 Fork the base homework repository: https://gitlab.igg.uni-bonn.de/teaching/cpp-homeworks-2018 To fork a repository in Git means to create a copy of the repository for your user 26

Submit homeworks through Git The address of your fork will be: /<your_name>/cpp-homeworks-2018 instead of: /teaching/cpp-homeworks-2018 To enable homework checks, from your fork: Settings Members Select members to invite Pick @hw_bot with developer rights This bot updates the Wiki in your project with evaluation of your homework Now push anything into the repo: git push origin master 27

How to see evaluation results Your repository has a Wiki page In a couple of minutes after a push open the wiki page Example look: 28

References Cpp Core Guidelines: https://github.com/isocpp/cppcoreguidelines Google Code Styleguide: https://google.github.io/styleguide/cppguide.html Git guide: http://rogerdudler.github.io/git-guide/ C++ Tutorial: http://www.cplusplus.com/doc/tutorial/ Book: Code Complete 2 by Steve McConnell 29