Presentation is loading. Please wait.

Presentation is loading. Please wait.

A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8.

Similar presentations


Presentation on theme: "A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8."— Presentation transcript:

1 A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8

2 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

3 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)

4  Container adaptors are made specifically to behave in a certain way  stack – last in first out  queue –first in first out

5 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

6 Map Map  std::map mymap; mymap[0]=“hello”;mymap[1]=“random”;  mymap.insert (0, “hello”));  std::map newmap; newmap.insert newmap.insert

7 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

8 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

9 <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”

10 <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”);

11 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


Download ppt "A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8."

Similar presentations


Ads by Google