Motivation Vector Files Other containers Homework 4. Vectors and STL Ján Dugáček October 10, 2018 Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Table of Contents 1 Motivation 2 Vector Exercise 3 Files Working with files The easy way Exercise 4 Other containers 5 Homework Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Motivation Can you write a program that gets 10 numbers and sorts them from the smallest up to the largest? Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Exercise Vector #i n c l u d e // . . . std : : vector numbers ; f o r ( i n t i = 0; i < 10; i++) { i n t read ; std : : c i n >> read ; numbers . push_back ( read ) ; } std : : cout << "At 3 i s : " << numbers [ 3 ] << std : : endl ; std::vector is a data structure that stores an indefinite number of elements It can store only one type of element, as defined in the < > brackets Elements are inserted using push_back Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Exercise Vector #2 f o r ( unsigned i n t i = 0; i < numbers . s i z e ( ) ; i++) { std : : cout << "At index " << i << " i s " << numbers [ i ] << std : : endl ; } Elements can be accessed through the square brackets Number of elements can be read using the length() method The first element is at index zero Accessing elements at negative indexes or after the last one causes undefined behaviour, which can mean random overwriting of variables or crashes Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Exercise Vector #3 f o r ( i n t& num : numbers ) { std : : cout << "We have " << num << std : : endl ; } The for can be shortened if we don’t need to know the index numbers . e r a s e ( numbers . begin () + 5 ) ; Erases element 5 Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Exercise Vector #4 #i n c l u d e " easy . h" // . . . easy : : vector vec ; Accessing elements at negative indexes or after the last one causes undefined behaviour, which can mean random overwriting of variables or crashes Use my easy::vector instead to receive warnings instead (at the cost of execution speed) Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Exercise Exercise 1 Have the user supply 5 numbers and output them afterwards 2 Have the user supply 5 numbers and output them in some other ordering 3 Have the user supply 5 numbers and then 1 number to set if he wants to get their arithmetic average, geometric average or harmonic average Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Working with files The easy way Exercise Working with files #i n c l u d e / . . . std : : ofstream out ( " output . dat " ) ; out << 42 << std : : endl ; The changes are written to disk when the variable ceases to exist Works much like std::cout, but is faster std : : i f s t r e a m i n ( " input . dat " ) ; i n t num ; i n >> num ; Works much like std::cin Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Working with files The easy way Exercise The easy way #i n c l u d e " easy . h" / . . . easy : : vector numbers ( "numbers . dat " , ’ \n ’ ) ; f o r ( f l o a t& num : numbers ) { std : : cout << "We have " << num << std : : endl ; } I have created this comfy library for you that does it easily The first argument is the file name, the second argument is the separator Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Working with files The easy way Exercise The easy way #2 #i n c l u d e " easy . h" // . . . easy : : vector > numbers ( "numbers . dat " , ’ \n ’ , ’ \ t ’ ) ; std : : cout << numbers [ 3 ] [ 4 ] << std : : endl ; It can also parse tables, the second argument is line separator, the third is the column separator Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Working with files The easy way Exercise The easy way #3 #i n c l u d e " easy . h" // . . . easy : : vector numbers ; // . . . easy : : w r i t e _ f i l e ( numbers , "numbers . dat " , ’ \n ’ ) ; // or numbers . w r i t e _ f i l e ( "numbers . dat " , ’ \n ’ ) ; So that you could also write files easily Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Working with files The easy way Exercise Exercise 1 Have the user supply numbers in a file, output them in order from smallest to greatest 2 Have the number supply numbers in a file and perform a linear fit, i. e. f(line_number) = a · line_number, you may assume the slope will be between 0.01 and 1000 Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Other containers 1 std::map allows indexing using nearly anything, but is slower (access time depends on the logarithm of size) 2 std::unordered_map is not so slow, but elements are read in a strange order 3 Accessing an unused location will create an uninitialised variable there #i n c l u d e // . . . std : : map a r r a y ; a r r a y [ " h i " ] = 3; a r r a y [ "zaphod" ] = 4; a r r a y [ " f o r d " ] = a r r a y [ "zaphod" ] − 1; f o r ( std : : pair & i t : a r r a y ) std : : cout << i t . f i r s t << "="<< i t . second << std : : endl ; Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Other containers 1 std::array is faster than vector, but it has a fixed size 2 Use easy::array to get boundary checking, it suffers from undefined behaviour like vector #i n c l u d e // . . . std : : array a r r a y ; // Has space f o r 4 elements a r r a y [ 3 ] = 3; a r r a y [ 2 ] = 4; std : : cout << "At 1 i s " << a r r a y [ 1 ] << std : : endl ; Ján Dugáček 4. Vectors and STL Motivation Vector Files Other containers Homework Homework Read numbers from a file and print them ordered, making sure the execution time is does not depend quadratically on the size of input The numbers will be between 0 and 1000000, ordered evenly over the range You have two weeks to do it Ján Dugáček 4. Vectors and STL