














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
L14: C++ STL. STL Containers ☹. ❖ STL containers store by value, not by reference. ▫ When you insert an object, the container makes a copy.
Typology: Study notes
1 / 22
This page cannot be seen from the preview
Don't miss anything!















C++’s Standard Library v C++’s Standard Library consists of four major pieces:
STL Containers L v STL containers store by value , not by reference § When you insert an object, the container makes a copy § If the container needs to rearrange objects, it makes copies
Our Tracer Class v Wrapper class for an unsigned int value_ § Default ctor, cctor, dtor, op=, op< defined § friend function operator<< defined § Also holds unique unsigned int id_ (increasing from 0 ) § Private helper method PrintID () to return "(id_,value_)" as a string § Class and member definitions can be found in Tracer.h and Tracer.cc v Useful for tracing behaviors of containers § All methods print identifying messages § Unique id_ allows you to follow individual instances
vector/Tracer Example
#include
STL iterator v Each container class has an associated iterator class ( e.g. vector
Type Inference (C++11) v The auto keyword can be used to infer types § Simplifies your life if, for example, functions return complicated types § The expression using auto must contain explicit initialization for it to work // Calculate and return a vector // containing all factors of n std::vector
auto and Iterators v Life becomes much simpler! for (vector
Updated iterator Example #include
STL Algorithms v A set of functions to be used on ranges of elements § Range: any sequence that can be accessed through iterators or pointers , like arrays or some of the containers § General form: v Algorithms operate directly on range elements rather than the containers they live in § Make use of elements’ copy ctor, =, ==, !=, < § Some do not modify elements
STL list v A generic doubly-linked list § http://www.cplusplus.com/reference/stl/list/ § Elements are not stored in contiguous memory locations
list Example #include #include
map Example void PrintOut (const pair<Tracer,Tracer>& p) { cout << "printout: [" << p.first << "," << p.second << "]" << endl; } int main (int argc, char** argv) { Tracer a, b, c, d, e, f; map<Tracer,Tracer> table; map<Tracer,Tracer>::iterator it; table. insert (pair<Tracer,Tracer>(a, b)); table[c] = d; table[e] = f; cout << "table[e]:" << table[e] << endl; it = table. find (c); cout << "PrintOut(it), where it = table.find(c)" << endl; PrintOut (it); cout << "iterating:" << endl; for_each (table. begin (), table. end (), & PrintOut ); return 0 ; }
Unordered Containers (C++11) v unordered_map, unordered_set § And related classes unordered_multimap, unordered_multiset § Average case for key access is O(1)