1:vector
#include <cstdlib> #include <iostream> #include <vector> using namespace std; int main(int argc, char *argv[]) { vector<int> vi(10); cout<<"vi size: "<<vi.size()<<endl; for(int i=0; i<5; i++) { vi[i] = i + 1; } vi.resize(5); for(int i=0; i<vi.size(); i++) { cout<<vi[i]<<endl; } }2:list
#include <cstdlib> #include <iostream> #include <list> using namespace std; int main(int argc, char *argv[]) { list<double> l; cout<<"l size: "<<l.size()<<endl; for(int i=0; i<5; i++) { l.push_back((i + 1) / 1000.0); } cout<<"l size: "<<l.size()<<endl; list<double>::iterator current = l.begin(); cout<<"Elements in l:"<<endl; while( current != l.end() ) { cout<<*current<<endl; current++; } current = l.begin(); current++; current++; l.insert(current, 0.1); cout<<"Elements in l:"<<endl; for(list<double>::iterator p = l.begin(); p != l.end(); p++) { cout<<*p<<endl; } }3:queue与stack
#include <cstdlib> #include <iostream> #include <stack> #include <queue> using namespace std; void StackUsage() { stack<double> s; for(int i=0; i<5; i++) { s.push(i/100.0); } cout<<"Elements in s:"<<endl; while( !s.empty() ) { double v = s.top(); s.pop(); cout<<v<<endl; } } void QueueUsage() { queue<int> q; for(int i=0; i<5; i++) { q.push(i + 1); } cout<<"Elements in q:"<<endl; while( !q.empty() ) { int v = q.front(); q.pop(); cout<<v<<endl; } } int main(int argc, char *argv[]) { StackUsage(); QueueUsage(); }4:algorithm
#include <cstdlib> #include <iostream> #include <vector> #include <algorithm> using namespace std; void current(int& v) { cout<<v<<endl; } void print(vector<int>& vec) { cout<<"Elements in vector:"<<endl; for_each(vec.begin(), vec.end(), current); } int compare(const int& a, const int& b) { return a < b; } int main(int argc, char *argv[]) { vector<int> v(10); for(int i=9; i>=0; i--) { v[i] = 9 - i; } print(v); sort(v.begin(), v.end(), compare); print(v); }