See this page as a slide show                 
The STL is the Standard Template Library. It wasn’t originally part of C++; it was a library written at HP. It’s now an official part of C++, but the name remains.                 
We will discuss these containers (there are more):                 
vector<char>
, with specialized methods
http://www.cplusplus.com is great:
There is no type called simply vector
. You can’t do this:
                
vector v;
c.cc:1: error: class template argument deduction failed:
It’s a vector of … what? You have to decide. Give it a type:                 
vector<int> a; vector<string> b; vector<const char *> c; cout << "Hooray!\n";
Hooray!
All STL containers have a current size, initially zero.                 
#include <vector> #include <string> #include <set> #include <iostream> using namespace std; int main() { vector<int> vi; string str; set<double> sd; cout << "vector size: " << vi.size() << '\n' << "string size: " << str.size() << '\n' << "set size: " << sd.size() << "\n\n"; vi.push_back(10); str.push_back('J'); str += "ck"; str.insert(1, "a"); sd.insert(7.6); sd.insert(1.2); sd.insert(7.6); cout << "vector size: " << vi.size() << '\n' << "string size: " << str.size() << '\n' << "set size: " << sd.size() << '\n'; }
vector size: 0 string size: 0 set size: 0 vector size: 1 string size: 4 set size: 2
vector
:                vector<int> v = {2018, 1492, 1957}; v.push_back(42); for (size_t i=0; i<v.size(); i++) cout << v[i] << '\n';
2018 1492 1957 42
Why size_t
? Why not just int
?
Because v.size()
returns an unsigned type, and the compiler will
complain if we compare signed and unsigned integers. size_t
is
an appropriate unsigned type.
                
[subscript] only works for vector
and string
.
Indexing would be expensive for set
and list
.
For map
, the subscript represents the key, and returns the value.
                
If you don’t need the index:                 
vector<int> v = {2018, 1492, 1957}; for (auto val : v) cout << val << '\n';
2018 1492 1957
set<int> s; s.insert(3); s.insert(1); s.insert(4); s.insert(1); s.insert(6); for (auto val : s) cout << val << ' ';
1 3 4 6
multiset<char> ms; ms.insert('b'); ms.insert('a'); ms.insert('n'); ms.insert('a'); ms.insert('n'); ms.insert('a'); for (auto val : ms) cout << val;
aaabnn
map<int, string> m; m[253] = "Jack Applin"; m[220] = "Wim Bohm"; m[270] = "Dave Matthews"; for (auto val : m) cout << val.second << " is teaching CS" << val.first << endl; cout << "CS253 is taught by " << m[253] << endl; cout << "CS222 is taught by " << m[222] << endl;
Wim Bohm is teaching CS220 Jack Applin is teaching CS253 Dave Matthews is teaching CS270 CS253 is taught by Jack Applin CS222 is taught by
Why did the for-loop produce the lines in that order?                 
list<int> l; for (int i=0; i<10; i++) l.push_back(rand() % 100); for (auto val : l) cout << val << ' ';
83 86 77 15 93 35 86 92 49 21
Write code that:
vector<int>
.
vector<int>
/etc/resolv.conf
into one big string
.
string
to a multiset<char>
.
multiset<char>
to a set<char>
.
.size()
and characters from the string
,
set<char>
, and multiset<char>
.
User: Guest