Contents |
end()-1
end()-1 yields arbitrary results
count(v.begin(), v.end(), n) > 0
#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;
}
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);
#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;
}
--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;
}