Topic 22: The C++ Standard  Library
Synopsis
	
-   Try not to...
  -   ...it's boring
  -   ...it's expensive
  -   ...it's suprisingly difficult
  -   ...it's unnecessary
	
 
	
-   I/O
  -   Character string manipulation
  -   Collections of objects (e.g. lists)
  -   Mathematical types and algorithms
	   (complex, min/max, permutations)
  -   Other algorithms (sort, search, etc.)
  - Support for Internationalization 
 
	
-   Pre-defined classes¤ that
	   provide useful components
  -   Part of the language standard so guaranteed
	   to be available everywhere (i.e. portable)
  -   Written by experts (efficient, reliable)
  -   Makes code more maintainable
  -   Previously known as the STL or Standard Template Library¤
 
	
-   Encapsulates a 
char* and the associated string-handling
	   functionality
  -   Conversion to and from 
char*
  -   Concatenation, append, insert, replace, substring extraction
  -   Copy constructor¤¤
  -   I/O
  -   Length and capacity
  -   Comparison
  -   Assignment
  -   Access to individual chars
  -   Search for substrings
	
 
#include <string>
using namespace std;
// AND LATER...
string s0;
string s1 = "cat";
string s2 = "alogue";
cout << s1 << s2 << endl;
s0 = s1 + s2;
int at = s0.find("log");
cout << s0.substr(at,3,5) << endl;
s1.replace(2,1,"nary");
char c = s2[4];
s2[4] = 'q';
	
-   Variations on a linear sequence of values
  -   Templated on the type of element they store
  -   Insertion and deletion
  -   Random access (but not to all container types)
  -   Pushing and popping to either end
  -   
class vector (general purpose sequence class¤)
  -   
class deque (optimized for pushing/popping at either end)
  -   
class list (optimized for sequential access and
		 inserting in the middle; no random access)
	
 
#include <deque>
using namespace std;
// AND LATER...
deque<string> names;
names.push_back("Shiranthi");
names.push_back("James");
names.push_back("Emily");
names.push_back("Damian");
names[2] = "Jesse";
for (int i=0; i<names.size(); i++)
{
	cout << names.at(i) << endl; // will throw exception if no element at i
}
//  or a better way to print elements
copy( names.begin(); names.end(); ostream_iterator<string>(cout,"\n")); // prints all elements separated by newlines
	
-   A sequence which allows us to use keys other than integers
  -   
map (give it a key, get/set a value)
  -   
multimap (a map supporting multiple values per key)
	
 
#include <map>
using namespace std;
// AND LATER
map<string,int>       mark_for;
mark_for["Damian"] = 100;
while (markfile >> name >> mark)
{
	mark_for[name] = mark;
}
while (cin >> name)
{
	cout << name << " scored " << mark_for[name] << endl;
}
	
-   
istream/ostream for terminal I/O
  -   
ifstream/ofstream for file I/O
  -   
istringstream/ostringstream for I/O ops into/out of strings
  -   Will cover next lecture
	
 
	
-   Implements complex numbers where each component is of
	   some specified type
  -   Possible component types are 
float, double, long double
  -   Implements all arithmetic ops and functions on complex
	   numbers
	
 
#include <complex>
using namespace std;
// AND LATER....
complex<double> minus1 (-1,0);
complex<double> i      (0,1);
complex<double> pi     (3.141592654,0);
complex<double> sqrtminus1 = sqrt(minus1);
if (i !=  sqrtminus1)
{
	cout << "Warning: local anomoly in spacetime!" << endl;
	cout << "Checking fundamental constants:" << endl;
	cout << exp(pi*i)-1 << " (should be 0)" << endl;
}
	
-   Act like pointers which can only step through
	   a container.
  -   Most containers allow you to step through values
	   by returning an iterator, which you then use
  -   "Find" operations on a container usually 
	   return an iterator, which steps through
	   all matching values
  -   Many generic¤ algorithms provided which make
	   use of iterators¤
	
 
#include <deque>
using namespace std;
// AND LATER...
deque<string> names;
names.push_back("Shiranthi");
names.push_back("James");
names.push_back("Emily");
names.push_back("Damian");
names[2] = "Jesse";
deque<string>::iterator next = names.begin();
deque<string>::iterator last = names.end();
for (; next != last; next++)
{
	cout << next->length() << endl;
}
#include <vector>
#include <algorithm>
using namespace std;
// AND LATER...
vector<double> heights = load_heights();
// TALL HEIGHTS
for (vector<double>::iterator next = heights.begin();
     next != heights.end();
     next++)
{
	if (*next > 2.00)
	{
		cout << (*next) << endl;
	}
}
// SHORTEST 10 HEIGHTS
sort(heights.begin(),heights.end());
vector<double>::iterator from = heights.begin();
vector<double>::iterator to = from+10;
for(;from != to && from != heights.end(); from++)
{
	cout << (*from) << endl;
}
	
-   Algorithms
  -   Localization
  -   Diagnostics and language support
  -   Standard exceptions¤
  -   Standard C components
	
 
- Stroustrup: Chapters 16 to 22
  
- Lippman & Lajoie: Chapters 6 and 12
  
- Josuttis, N. M. The C++ Standard Library: A Tutorial and Reference,
  Addison-Wesley 1999. 
 
  This material is part of the CSE2305 - Object-Oriented 
  Software Engineering course.
  Copyright © Jon McCormack & Damian Conway, 1998–2005. All rights 
  reserved.