a recap of the stl and more containers plus an intro to string and file input and output! lecture 8

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

Upload: reginald-gregory

Post on 18-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

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)

TRANSCRIPT

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

A recap of the STL and A recap of the STL and more containersmore containers

Plus an intro to string and file Plus an intro to string and file input and output!input and output!

Lecture 8Lecture 8

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

Standard template library (STL)Standard template library (STL) General purpose library of data structures General purpose library of data structures

and algorithmsand algorithms Everything in the STL is a templateEverything in the STL is a template Containers e.g. <map> <vector> <list>Containers e.g. <map> <vector> <list> Algorithms e.g. sort, find, for_each, searchAlgorithms e.g. sort, find, for_each, search IteratorsIterators

Generalization of pointersGeneralization of pointers Access data within containersAccess data within containers Reverse, Forward and randomReverse, Forward and random

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

Sequential containersSequential containers Containers:Containers:

std::vector<int> v (dynamic array)std::vector<int> v (dynamic array) std::list <int> l (doubly linked list)std::list <int> l (doubly linked list) std::deque<int> dq (double ended queue)std::deque<int> dq (double ended queue)

Adaptors:Adaptors: std::queue<int> qstd::queue<int> q std::stack<int> sstd::stack<int> s std::priority_queue<int> pq (A heap)std::priority_queue<int> pq (A heap)

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

Container adaptors are made specifically Container adaptors are made specifically to behave in a certain wayto behave in a certain way

stack – last in first outstack – last in first out queue –first in first outqueue –first in first out

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

Associative containersAssociative containers

Set – Unique keys, specific order, think of Set – Unique keys, specific order, think of it as a binary search treeit as a binary search tree std::set<int> first;std::set<int> first; std::set<int, comparison function> first; std::set<int, comparison function> first;

Map – Unique keys, specific order, key Map – Unique keys, specific order, key value pair, binary search tree value pair, binary search tree implementationimplementation Operator [] allows access to get value by keyOperator [] allows access to get value by key

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

Map<Type, Key>Map<Type, Key>

std::map<int, string> mymap;std::map<int, string> mymap;mymap[0]=“hello”;mymap[0]=“hello”;mymap[1]=“random”;mymap[1]=“random”;

mymap.insert<std::pair<int, string>(0, mymap.insert<std::pair<int, string>(0, “hello”));“hello”));

std::map newmap;std::map newmap;newmap.insert<mymap.begin(), mymap.find(1)>newmap.insert<mymap.begin(), mymap.find(1)>

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

Multi key containersMulti key containers

std::<multiset> msstd::<multiset> ms Same as a set but allows keys of the same Same as a set but allows keys of the same

value to existvalue to exist std::<multimap> mmstd::<multimap> mm

Same as a map but allows multiple keys of Same as a map but allows multiple keys of the same valuethe same value

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

StreamsStreams (part of the standard (part of the standard C++ library)C++ library)

<iostream> – input output stream objects<iostream> – input output stream objects ostream – output stream objectsostream – output stream objects istream – input stream objectsistream – input stream objects

istreamistream ifstream – input file streamifstream – input file stream istringstream – input string streamistringstream – input string stream

ostreamostream ofstream – output file streamofstream – output file stream ostringstream – output string streamostringstream – output string stream

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

<sstream><sstream> Provides both input and outputProvides both input and output << input data<< input data >> output data>> output data

std::stringstream ss;std::stringstream ss;ss << 5 << “hello”;ss << 5 << “hello”;int f;int f;string s;string s;ss >> f; //now has value 5ss >> f; //now has value 5ss >> s; //now has value “hello”ss >> s; //now has value “hello”

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

<fstream><fstream>

Similar to sstreamSimilar to sstream

ofstream myfile;ofstream myfile;myfile.open(“test.txt”); //open a filemyfile.open(“test.txt”); //open a filemyfile << “hello word\n”; //write into the filemyfile << “hello word\n”; //write into the filemyfile.close(); //close the filemyfile.close(); //close the file

ofstream myfile (“test.txt”);ofstream myfile (“test.txt”);

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

Reading from fileReading from file

ifstream myfile;ifstream myfile;myfile.open(“test.txt”); //open a filemyfile.open(“test.txt”); //open a filestring line;string line;while ( getline (myfile,line) ) while ( getline (myfile,line) )

{ { cout << line << '\n'; cout << line << '\n'; }} myfile.close(); myfile.close(); myfile.close(); //close the filemyfile.close(); //close the file