Bruce Merry. IOI Training Dec 2013

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

C++ basics Getting started with, and Data Types.

Programming in C++: Assignment Week 1

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:

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:

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS3157: Advanced Programming. Outline

CSE 303: Concepts and Tools for Software Development

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

a data type is Types

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

CSE 333 Autumn 2013 Midterm

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

Chapter 15 - C++ As A "Better C"

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

Getting started with C++ (Part 2)

Pointers and References

CS24 Week 3 Lecture 1

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

III. Classes (Chap. 3)

Introduction to C++ Systems Programming

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

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++

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

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

A brief introduction to C programming for Java programmers

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

CS102 C++ Exception Handling & Namespaces

Computer Science II Lecture 2 Strings, Vectors and Recursion

Lab # 02. Basic Elements of C++ _ Part1

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

G52CPP C++ Programming Lecture 17

Topics. bool and string types input/output library functions comments memory allocation templates classes

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Intermediate Programming, Spring 2017*

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

G52CPP C++ Programming Lecture 16

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

WARM UP LESSONS BARE BASICS

Introduction to Programming using C++

Chapter 2. Procedural Programming

Financial computing with C++

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CA31-1K DIS. Pointers. TA: You Lu

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

377 Student Guide to C++

Interview Questions of C++

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

C++ Undefined Behavior What is it, and why should I care?

CS11 Introduction to C++ Fall Lecture 1

Input And Output of C++

Example Final Questions Instructions

Object-Oriented Programming for Scientific Computing

2 nd Week Lecture Notes

C++ Programming Lecture 2 Software Engineering Group

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

CMPS 221 Sample Final

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

Scientific Computing

Computer Programming : C++

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

C++ Templates. David Camp

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

A brief introduction to C++

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

Introduction to C++ Introduction to C++ 1

COMSW Introduction to Computer Programming in C

Ch. 12: Operator Overloading

CSC102 INTRO TO PROGRAMMING WITH PYTHON LECTURE 28 OOPS IN C++ MICHAEL GROSSBERG

COMP322 - Introduction to C++

Lab Instructor : Jean Lai

CS349/SE382 A1 C Programming Tutorial

CSE 333 Lecture 9 - intro to C++

Protection Levels and Constructors The 'const' Keyword

Engineering Tools III: OOP in C++

Transcription:

IOI Training Dec 2013

Outline 1 2 3

Outline 1 2 3

You can check that something is true using assert: #include <cassert> int main() { assert(1 == 2); } Output: test_assert: test_assert.cpp:4: int main(): Assertion 1 == 2 failed.

Disabling To disable assertions, add #define NDEBUG as the first line of your source.

Caution #define NDEBUG #include <cassert> #include <iostream> using namespace std; bool foo() { cout << "In foo\n"; return true; } int main() { assert(foo()); }

Caution #define NDEBUG #include <cassert> #include <iostream> using namespace std; bool foo() { cout << "In foo\n"; return true; } int main() { assert(foo()); } When assertions are disabled, the expression is not evaluated.

Outline 1 2 3

String-To-Integer Conversions Use istringstream to treat a string as an input stream: #include <sstream> int x; istringstream stream("123"); stream >> x; // Now x == 123

String-To-Integer Conversions You can reduce typing by using a C function instead: #include <cstdlib> string xstr = "123"; string ystr = "12345678912345678"; int x = atoi(xstr.c_str()); long long y = atoll(ystr.c_str());

String-To-Integer Conversions C++11 has a more convenient wrapper: #include <string> string xstr = "123"; string ystr = "12345678912345678"; int x = stoi(xstr); long long y = stoll(ystr);

Integer-To- The general solution is ostringstream: #include <sstream> ostringstream o; o << 123; string s = o.str(); // s == "123"

Integer-To- C++11 again has a convenience wrapper #include <string> string s = to_string(123);

Outline 1 2 3

Introduction In Java and Python, all objects are references: Passing to a function is cheap: just another reference Callee function can modify the object Every object must be explicitly created (e.g., with new)

C++ Default By default, C++ objects are values:

C++ Default By default, C++ objects are values: void foo(vector<string> grid) { // foo operates on a *copy* of grid }

C++ Default By default, C++ objects are values: void foo(vector<string> grid) { // foo operates on a *copy* of grid } string mystrings[4]; // array contains 4 empty strings

Reference arguments To make a parameter a reference, prefix it with &: void foo(vector<string> &grid) { // foo now operates on the original grid }

Reference arguments To make a parameter a reference, prefix it with &: void foo(vector<string> &grid) { // foo now operates on the original grid } Can also qualify references as const: void foo(const vector<string> &grid) { // foo is prevented from modifying grid }

Reference Variables Variables can be references, but they cannot be changed: vector<string> strings(5); string &first = strings[0]; string &second = strings[1]; string &something; // error first += "hello"; // appends to strings[0] // copy one *string* to another: second = first;

Pointers Pointers are similar to references Can be changed to point at other things Can be null pointers Syntax is more roundabout Avoid them for now

Outline 1 2 3

Can define shorthand for other types: typedef long long ll; typedef vector<vector<ll> > vvll;... // declare a vector<vector<long long> >: vvll myarray;

Outline 1 2 3

Improving Read Performance Add ios::sync_with_stdio(false) to the start of your program to improve cin performance. Table : Input performance (time to read 10 7 integers) Method Time (s) cin 2.70 cin with tweak 0.78 scanf 0.84

Improving Read Performance Add ios::sync_with_stdio(false) to the start of your program to improve cin performance. Table : Input performance (time to read 10 7 integers) Method Time (s) cin 2.70 cin with tweak 0.78 scanf 0.84 Side effect: do not mix cin and scanf

Improving Write Performance What is the difference between these two lines? cout << 123 << endl; cout << 123 << \n ;

Improving Write Performance What is the difference between these two lines? cout << 123 << endl; cout << 123 << \n ; Using endl flushes the output.

Improving Write Performance What is the difference between these two lines? cout << 123 << endl; cout << 123 << \n ; Using endl flushes the output. Table : Output performance (time to write 10 7 integers) Method Time (s) endl 2.34 \n 0.75 printf 0.80

Outline 1 2 3

Warnings -Wall Provide lots of helpful warnings -W Provide even more warnings, some useless

Optimisation Use -O2 to optimize your code Speedup varies a lot, depending on code

Optimisation Use -O2 to optimize your code Speedup varies a lot, depending on code Interferes with debugging tools

Optimisation Use -O2 to optimize your code Speedup varies a lot, depending on code Interferes with debugging tools Undefined behaviour can change

Optimisation Use -O2 to optimize your code Speedup varies a lot, depending on code Interferes with debugging tools Undefined behaviour can change Some warnings only work with optimisation

Optimisation Use -O2 to optimize your code Speedup varies a lot, depending on code Interferes with debugging tools Undefined behaviour can change Some warnings only work with optimisation Can also do -O3, but has diminishing returns

Outline 1 2 3

Including The Standard Libraries This will pull in all the standard library headers #include <bits/stdc++.h> It does make compilation quite slow.

Outline 1 2 3

Uninitialized Data int x; int y[3]; vector<int> z(4); cout << x << << y[1] << << z[2]; Which values are well-defined?

Uninitialized Data The following are generally safe: Classes with a constructor, if the constructor explicitly initialises all fields. STL containers like vector (even for primitive types) Primitive types are undefined when: Declared directly Declared in an array Declared in a struct/class and not set by constructor

Out-of-range Array Access int x[3] = {1, 2, 3}; x[3] = 4; Anything can happen here!

to Local Variables Do not try to return containers by reference: vector<int> &foo(int n) { vector<int> ans; for (int i = 0; i < n; i++) ans.push_back(i); return ans; }

to Local Variables Do not try to return containers by reference: vector<int> &foo(int n) { vector<int> ans; for (int i = 0; i < n; i++) ans.push_back(i); return ans; } Return by value

to Local Variables Do not try to return containers by reference: vector<int> &foo(int n) { vector<int> ans; for (int i = 0; i < n; i++) ans.push_back(i); return ans; } Return by value GCC will optimise it

Outline 1 2 3

Mod on Negative Values 5 % 3 == 2-5 % 3 == -2 (unlike Python) When a problem asks for an answer modulo M: ans %= M; if (ans < 0) ans += M;

Unsigned Is Evil What is wrong with this code? // One pass of bubblesort for (int i = 0; i < arr.size() - 1; i++) if (arr[i] > arr[i + 1]) swap(arr[i], arr[i + 1]);

Unsigned Is Evil What is wrong with this code? // One pass of bubblesort for (int i = 0; i < arr.size() - 1; i++) if (arr[i] > arr[i + 1]) swap(arr[i], arr[i + 1]); If arr is empty, then arr.size() - 1 wraps around.

Stack Overflow Function parameters and local variables kept on a stack Stack size limits possible recursion depth Linux defaults to an 8 MiB stack! So be careful with more than 100 000 levels of recursion.