Standard C++ Library and Standard Template Library (STL)
Sequence Containers:
Vector
#include < vector >
using namespace std;
int main()
{
vector< int > v(10);
//Fill vector
for (int i=0; i<10; ++i)
v[i]=i;
//Print vector
for (vector< int >::iterator i = v.begin(); i != v.end(); ++i)
cout << *i << '\t';
cout << endl;
return 0;
}
More Vector
#include < vector >
using namespace std;
int main()
{
vector< int > v;
v.reserve(10);
//Fill vector
for (int i=0; i<10; ++i)
v.push_back(i*i);
//Print vector
vector< int >::iterator cur = v.begin();
vector< int >::iterator end = v.end();
while (cur != end)
{
cout << *cur << '\t';
cur++;
}
cout << endl;
//Sum vector elements
int sum = 0;
cur = v.begin();
while (cur != end)
{
sum += *cur;
cur++;
}
cout << "Sum is: " << sum << endl;
return 0;
}
Deque (that's double-ended queue, as you all know)
#include < vector >
#include < deque >
using namespace std;
int main()
{
vector< int > v(5, 1); //all elements initialized to 1
int data[] = {2, 3, 4, 5, 6};
deque< int > d(data, data+5); //note pointer = iterator range
//List deque
cout << "Values in deque:" << endl;
deque< int >::iterator p;
for (p=d.begin(); p != d.end(); ++p)
cout << *p << '\t';
cout << endl;
//Insert contents of a vector in deque
//Need to specify location and range: where, from and to
d.insert(d.begin(), v.begin(), v.end()); //note iterator range
//List deque
cout << "After inserting vector of 1's: " << endl;
for (p=d.begin(); p != d.end(); ++p)
cout << *p << '\t';
cout << endl;
return 0;
}
List
#include < list >
#include < numeric > //for accumulate
using namespace std;
void print(list< double > &lst)
{
list< double >::iterator p;
for( p=lst.begin(); p != lst.end(); ++p)
cout << *p << '\t';
cout << endl;
}
int main()
{
int data[] = {0.9, 0.8, 88, -9.9 };
list< double > nums;
for( int i=0; i<4; ++i)
nums.push_front(data[i]);
//Print list
print(nums);
//Sort list
nums.sort();
//Print again
print(nums);
//Add numbers - note accumulator intialization
cout << "Sum is: "
<< accumulate(nums.begin(), nums.end(), 0.0) << endl;
return 0;
}
Associative Containers:
Map
#include < string >
#include < map >
using namespace std;
int main()
{
//Build map, keys are strings, values are ints
map< string, int, less< string > > lookup;
lookup["Breznay, Peter"] = 25;
lookup["Crawford, Cindy"] = 30;
lookup["Pitt, Brad"] = 27;
lookup["Lopez, Jennifer"] = 26;
//Find values based on keys
cout << "Peter is " << lookup["Breznay, Peter"] << " years old." << endl;
cout << "Cindy is " << lookup["Crawford, Cindy"] << " years old." << endl;
cout << "Brad is " << lookup["Pitt, Brad"] << " years old." << endl;
return 0;
}
Container Adapters:
Stack
#include < string >
#include < vector >
#include < stack >
using namespace std;
int main()
{
//Build stack of strings, using vector container
stack< string, vector< string > > strStack;
string quote[] = { "The wheel that\n", "squeaks the loudest\n",
"is the one that\n", "gets the grease.\n" };
for (int i=0; i<4; ++i)
strStack.push(quote[i]);
while (!strStack.empty())
{
cout << strStack.top();
strStack.pop();
}
return 0;
}