CS253

CS253: Software Development with C++                

Spring 2017                

STL                

See this page as a slide show                 

CS253 STL                

STL                

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:                 

Incomplete type                

There is no type called vector. You can’t do this:                 

    vector v;

It’s a vector of … what? You have to decide. Give it a type:                 

    vector<int> a;
    vector<string> b;
    vector<const char *> c;

size                

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 << vi.size() << str.size() << sd.size() << '\n';
    vi.push_back(10);
    str.push_back('J'); str+='a'; str += "ck";
    sd.insert(7.6); sd.insert(1.2); sd.insert(7.6);
    cout << vi.size() << str.size() << sd.size() << '\n';
}
000
142

Iterating over a vector:                

vector<int> v = {2016, 1492, 1957};
v.push_back(42);
for (size_t i=0; i<v.size(); i++)
    cout << v[i] << '\n';
2016
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 return the value.                 

Easier iteration                

If you don’t need the index:                 

vector<int> v = {2016, 1492, 1957};
for (auto val : v)
    cout << val << '\n';
2016
1492
1957

set                

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                

multiset<int> ms;
ms.insert(3);
ms.insert(1);
ms.insert(4);
ms.insert(1);
ms.insert(6);
for (auto val : ms)
    cout << val << ' ';
1 1 3 4 6 

map                

map<int, string> m;
m[253] = "Jack Applin";
m[200] = "Wim Bohm";
m[270] = "Sanjay Rajopadhye";
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 CS200
Jack Applin is teaching CS253
Sanjay Rajopadhye is teaching CS270
CS253 is taught by Jack Applin
CS222 is taught by 

Why did the for-loop produce the lines in that order?                 

Linked List                

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 

Now …                

Write code that:

  1. Reads integers, until the user enters zero, into a vector<int>.
  2. Write the integers from the vector<int>
  3. Read characters from the file /etc/resolv.conf into one big string.
  4. Copy the characters from the string to a multiset<char>.
  5. Copy the characters from the multiset<char> to a set<char>.
  6. Display the .size() and characters from the string, set<char>, and multiset<char>.
  7. Explain why they’re different.

Modified: 2017-01-26T22:03                 

User: Guest                 

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building