Motivation Vector Files Other containers Homework 4. Vectors and STL Jan Dugacek October 9, 2019 Jan Dugacek 4. Vectors and STL Motivation Vector • Exercise Files • Working with files • The easy way 9 Exercise Other containers Homework Jan Dugacek 4. Vectors and STL Motivation Vector Files Other containers Homework Can you write a program that gets 10 numbers and sorts them from the smallest up to the largest? Jan Dugacek 4. Vectors and STL Motivation Vector Files Other Containers Homework #i n cIu d e //••• std : : vecto r numbers; for (int i = 0; i < 10; i++) { int read; std : : ein » read; numbers . push_back( read ) ; } std : : cout « "At 3 « 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 Jan Dugäcek 4. Vectors and STL Motivation Vector Files Other containers Homework for (unsigned int i = 0; i < n u m bers . s i z e () ; i++) { std::cout « "At index " « i « " is " « numbers [ i ] « std : : end I ; } • Elements can be accessed through the square brackets o Number of elements can be read using the lengthO 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 Jan Dugacek 4. Vectors and STL Motivation Vector Files Other containers Homework for (int& num numbers) { std : : cout « "We have " « num « std : : end I ; } 9 The for can be shortened if we don't need to know the index numbers . erase ( numbers . begi n () + 5); 9 Erases element 5 Jan Dugacek 4. Vectors and STL Motivation Vector Files Other containers Homework Exercise #i n cIu d e "easy . h a... ii 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) Jan Dugacek 4. Vectors and STL Motivation Vector Files Other containers Homework Exercise 1 O Have the user supply 5 numbers and output them afterwards © Have the user supply 5 numbers and output them in some other ordering O 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 □ S1 Jan Dugacek 4. Vectors and STL Motivation Vector Files Other containers Homework Working with files The easy way Exercise ^include /••• std : : ofstream out(" output . dat" ); out « 42 « std : : end I ; • The changes are written to disk when the variable ceases to exist <* Works much like std: :cout, but is faster std :: ifstream in("input.dat"); i n t num ; i n » num ; • Works much like std: :cin Jan Dugacek 4. Vectors and STL Motivation Vector Files Other containers Homework Working with files The easy way Exercise #i n cIu d e "easy.h " /••• easy :: vector n u m bers ( " n u m bers . d at " , '\n'); for (floats 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 Jan Dugacek 4. Vectors and STL ;y way Motivation Vector Files Other containers Homework rking with files e easy way reise #i n cIu d e "easy.h " //■■■ easy:: vector numbers; //... easy : : write_file (numbers , "numbers, dat" , //or numbers. write_file ("numbers, dat" , '\n ' ); So that you could also write files easily Jan Dugacek 4. Vectors and STL Motivation Vector Files Other containers Homework Working with files The easy way Exercise Have the user supply numbers in a file, output them in order from smallest to greatest 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 Jan Dugacek 4. Vectors and STL Motivation Vector Files Other containers Homework std: :map allows indexing using nearly anything, but is slower (access time depends on the logarithm of size) std: :unordered_map is not so slow, but elements are read in a strange order Accessing an unused location will create an uninitialised variable there ^include //... std ::map array; array["hi"] = 3; a r ra y [ 1 za phod1 ] = 4 ; array[ ] = a r r a y [ 1 za phod 1 ] — 1; for (std ::pair& i t std : : cout « it . f i rst « "="« it array) second « std e n d I ; Jan Dugacek 4. Vectors and STL Motivation Vector Files Other containers Homework O std: : array is faster than vector, but it has a fixed size O Use easy: :array to get boundary checking, it suffers from undefined behaviour like vector ^include //••• std::array array; a rray[3] = 3; a rray [2] = 4 ; std : : cout « "At 1 is " « array [1] « std : : end I ; Jan Dugacek 4. Vectors and STL • Read numbers from a file and print them ordered • The numbers will be between 0 and 1000000, ordered evenly over the range o You have two weeks to do it • Challenge: Write it in a way that the execution time is does not depend quadratically on the size of input Jan Dugacek 4. Vectors and STL