03-Introducing-The-STL

03-Introducing-The-STL - CS107 Spring 2007 Handout 03 April...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 03 Spring 2007 April 4, 2007 C++’s Standard Template Library (STL) My intent here is to provide a gentle introduction to some of the container classes defined by the STL. My personal experience is that the pair , the vector and the map are used more often than the other STL classes, so I’ll frame my discussion around them. After reading through this, you might bookmark two publicly which explain (or at least document) all of the various components of the STL. Those two URLS are: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp http://www.sgi.com/tech/stl The pair The STL pair is nothing more than a template struct with two fields. template <class U, class V> struct pair { U first; V second; first(first), second(second) {} }; template <class U, class Y> pair<U, V> make_pair(const U& first, const V& second); Notice that the pair is a struct rather than a class . When handling a pair , you’re free to directly access the first and second fields, since there are no private access modifiers in place to prevent you. (Had it been declared a class , the first and second fields would have been private by default. We don’t need to encapsulate the fields of a pair , because it’s hardly a secret what a pair really is.) The details of the pair are trivial, but they deserve specific mention up front, because many other STL classes depend on them. All of the associative containers ( map , hash_map , and multimap ) require a pair be used to insert new data. map<string, int> portfolio; portfolio.insert( make_pair (string("LU"), 400)); portfolio.insert( make_pair (string("AAPL"), 80)); portfolio.insert( make_pair (string("GOOG"), 6500)); The calls to insert do what you’d expect. After the third insertion, our stock portfolio consists of 400 shares of Lucent stock, 80 shares of Apple stock, and 6500 shares of Google. We’ll talk more about the map in a few paragraphs. The point here is that the pair comes up at least as often as the map does.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 The vector The vector is a type-safe, sequential container class that behaves like an array. You can set the size of the vector up front, you can use operator[] to access and modify individual entries, and you can splice new elements in anywhere you want and let the vector do all of the shifting for you. The primary win of the vector over the array comes from its ability to grow and shrink automatically in response to insertion and deletion. Because arrays are useful data structures all by themselves, and because vector s are designed to not only behave like arrays but to interact with the programmer using the same syntax, vector s are used more often than any other STL class. Programs making use of the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 8

03-Introducing-The-STL - CS107 Spring 2007 Handout 03 April...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online