A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8
Standard template library (STL) General purpose library of data structures and algorithms Everything in the STL is a template Containers e.g. Containers e.g. Algorithms e.g. sort, find, for_each, search Iterators Generalization of pointers Access data within containers Reverse, Forward and random
Sequential containers Containers: std::vector v (dynamic array) std::list l (doubly linked list) std::deque dq (double ended queue) Adaptors: std::queue q std::stack s std::priority_queue pq (A heap)
Container adaptors are made specifically to behave in a certain way stack – last in first out queue –first in first out
Associative containers Set – Unique keys, specific order, think of it as a binary search tree std::set first; Map – Unique keys, specific order, key value pair, binary search tree implementation Operator [] allows access to get value by key
Map Map std::map mymap; mymap[0]=“hello”;mymap[1]=“random”; mymap.insert (0, “hello”)); std::map newmap; newmap.insert newmap.insert
Multi key containers std:: ms Same as a set but allows keys of the same value to exist std:: mm Same as a map but allows multiple keys of the same value
Streams(part of the standard C++ library) – input output stream objects ostream – output stream objects istream – input stream objects istream ifstream – input file stream istringstream – input string stream ostream ofstream – output file stream ostringstream – output string stream
<sstream> Provides both input and output << input data >> output data std::stringstream ss; ss << 5 << “hello”; int f; string s; ss >> f; //now has value 5 ss >> s; //now has value “hello”
<fstream> Similar to sstream ofstream myfile; myfile.open(“test.txt”); //open a file myfile << “hello word\n”; //write into the file myfile.close(); //close the file ofstream myfile (“test.txt”);
Reading from file ifstream myfile; myfile.open(“test.txt”); //open a file string line; while ( getline (myfile,line) ) { cout << line << '\n'; } myfile.close(); myfile.close(); //close the file