## vector<>

• The last element in the vector is `end()-1`
• Iterating beyond `end()-1` yields arbitrary results
• Check for membership of n in v: `count(v.begin(), v.end(), n) > 0`

## Examples

### List Iteration

```#include <iostream>
#include <list>
#include <string>
using namespace std;

class Thing {
public:
Thing(string text): text(text) {}

protected:
string text;

// is declared as friend because it accesses 'text'
friend ostream& operator <<(ostream& os, const Thing* t);

};

ostream& operator <<(ostream& os, const Thing* t) {
return os << t->text << endl;
}

int main(void) {
list<Thing *> l;
for(int n = 0; n < 3; n++)
l.push_back(new Thing("Thingie"));

for(list<Thing *>::iterator i = l.begin(); i != l.end(); ++i)
cout << *i;

return 0;
}
```

### Sorting

```
bool ordering_relation(Thing *a, Thing *b) {
return a->value < b->value;
}

vector<Thing> things = new vector<Thing>();

// sort does not maintain order of elements that are equal
sort(things.begin(), things.end(), ordering_relation);

// stable_sort maintains order of equal elements
stable_sort(things.begin(), things.end(), ordering_relation);

```

### Complex Number

```#include <complex>
#include <iostream>
using namespace std;

int main(void) {
complex<float> x(1,2);
complex<float> y = x / 2.f;
//y = x / 2.0; error: division with double
//y = x / 2;   error: division with int
cout << sin(y) << endl;
cout << norm(y) << endl;
return 0;
}
```

## Don't Inherit from STL Classes

It is a common question, "Why shouldn't we inherit a class from STL classes?" The answer to this question is simple—the Big Three rule [CLI95]. In other words, you should have a virtual destructor in a class if you want to make it a base class. However, there isn't any class in STL that defines a virtual destructor. Even if you try to inherit a class from STL class, the compiler doesn't complain about it, although there is a high chance of a resource leak in the program.

--quote from codeguru

Compose instead, e.g. include a list instance in your PrintableList template class:

```#include <vector>
#include <iostream>
#include <string>
using namespace std;

template<class T> class PrintableList {
public:
operator string();
void operator <<(T stuff) { l.push_back(stuff); }
protected:
vector<T> l;
};

template<class T> PrintableList<T>::operator string() {

// vector<T>::iterator i;       // this does not parse, even though it
// makes sense
typename vector<T>::iterator i; // 'typename' kicks the ass of the parser
// to make it understand that it is a
// type name and not some other arbitrary
// stuff. C++ is kinda stupid that way.
string s("[");
for(i = l.begin(); i != l.end()-1; ++i) // here it would make sense to
s += string(*i) + ", ";             // use the STL algorithm
return s + string(*i++) + "]";          // 'accumulate'. Unfortunately,
}                                           // it didn't exist in my GNU C++
// library at the time of writing.
int main(void) {
PrintableList<string> l;
l << "hewey"; l << "dewey"; l << "luie";
cout << string(l);
return 0;
}
```