zSTL - STL 2010DavidA.Smallberg#include using...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 STL © 2010 David A. Smallberg #include <stack> using namepsace std; stack<int> si; si.push(10); si.push(20); if ( !si.empty() ) cout << si.top(); // writes 20 si.pop(); cout << si.size(); // writes 1 cout << si.top(); // writes 10 #include <queue> using namespace std; queue<int> qi; qi.push(10); qi.push(20); if ( !qi.empty() ) cout << qi.front(); // writes 10 cout << qi.back(); // writes 20 qi.pop(); cout << qi.size(); // writes 1 cout << qi.front(); // writes 20 © 2010 David A. Smallberg
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 #include <vector> using namespace std; vector<int> vi; vi.push_back(10); vi.push_back(20); vi.push_back(30); cout << vi.size(); // writes 3 cout << vi.front(); // writes 10 cout << vi.back(); // writes 30 vi[1] = 40; // vi[3] = 50; would be undefined behavior for (size_t k = 0; k < vi.size(); k++) cout << vi[k] << endl; // writes 10 40 30, one per line vi.pop_back(); for (size_t k = 0; k < vi.size(); k++) cout << vi[k] << endl; // writes 10 40, one per line vi.at(1) = 60; vi.at(3) = 70; // throws exception vector<double> vd(10); // vd.size() is 10, each element is 0.0 vector<string> vs(10, "Hello"); // vs.size() is 10, each element is "Hello" int a[5] = { 10, 20, 30, 40, 50 }; vector<int> vx(a, a+5); // vx.size() is 5, vx[0] is 10, vx[1] is // 20, . .., vx[4] is 50 vector<int> vi; vi[0] = 10; // undefined! vi.size() is 0 // there are no elements © 2010 David A. Smallberg #include <list> using namespace std; list<int> li; li.push_back(20);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

zSTL - STL 2010DavidA.Smallberg#include using...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online