CS253 STL                
Purpose                
A video introduction is available.
For this lab, you will create a program called
results.cc
and turn it in. First, some STL knowledge.
                
Overview                
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):
                
Documentation                
https://cplusplus.com is great:
Incomplete type                
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!
.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 << "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
Iterating over a vector:                
vector<int> v = {2024, 1492, 1957};
v.push_back(42);
for (size_t i=0; i<v.size(); i++)
cout << v[i] << '\n';
2024
1492
1957
42
Why size_t? Why not just int?
Because vector::size() returns an unsigned type, and the compiler will
complain if we compare signed and unsigned integers. size_t is
an appropriate unsigned type.
                
vector<int> v;
for (int i=0; i<v.size(); i++)
cout << v[i] << '\n';
c.cc:2: warning: comparison of integer expressions of different signedness:
‘int’ and ‘std::vector<int>::size_type’ {aka ‘long unsigned
int’}
[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.
                
Easier iteration                
If you don’t need the index:
                
vector<int> v = {2024, 1492, 1957};
for (auto val : v)
cout << val << '\n';
2024
1492
1957
set                
set<char> s;
for (char c : "bingeing"s)
s.insert(c);
for (auto val : s)
cout << val;
begin
- You recall that
"bingeing"s
is a C++ string object,
not an old null-terminated C string.
- How did “bingeing” become “begin”⁇
multiset                
multiset<char> ms = {'l', 'o', 'y', 'a', 'l'};
for (auto val : ms)
cout << val;
alloy
map                
map<char, string> grades = {
{'W', "Withdraw"},
{'A', "Excellent"},
{'B', "Better than average"},
{'C', "OK"},
{'D', "Not passing"},
{'F', "Failure"},
};
grades['I'] = "Incomplete";
for (auto p : grades)
cout << p.first << " means " << p.second << '\n';
cout << "If you get it all right, you are: " << grades['A'] << '\n';
cout << "In CS253 last year, I got a Q: " << grades['Q'] << '\n';
A means Excellent
B means Better than average
C means OK
D means Not passing
F means Failure
I means Incomplete
W means Withdraw
If you get it all right, you are: Excellent
In CS253 last year, I got a Q:
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 …                
Create a program results.cc
. We will compile it on a CS Dept.
computer with g++ -std=c++17 -Wall results.cc
, which must not
produce any warnings or errors. The program must:
                
- Reads integers, until the user enters zero, into a
vector<int>
. Don’t put the zero in the vector.
No prompting or error-checking is necessary.
- Display the integers from the
vector<int>
, one per line.
- Read all the characters from the file
/etc/resolv.conf
into one
big string. By “all the characters”, I mean everything that was in
the file—letters, numbers, punctuation, spaces, newlines, etc.:
every single byte.
- Copy all the characters from the string to a
multiset<char>
.
- Copy all the characters from the
multiset<char>
to a set<char>
.
- Display the
.size()
and characters from the string,
set<char>
, and multiset<char>
, like this:
string: size=
length stringcontentswithnoextrapadding
set: size=
length setcontentswithnoextrapadding
multiset: size=
length multisetcontentswithnoextrapadding
For all three bunches of output, write the container type, as shown,
then size=
, the number of characters in the container, one space,
then everything in the container, with no extra spaces, commas,
or any extra separators between the characters, then add a newline.
The file contains newlines, so it won’t look pretty.
-
Add a comment block to your program, starting with
“
// QUESTION 7
”, that explains why the sizes from the previous
question aren’t all the same. I want an explanation—merely
stating “this one is 10, but this one is 20” is not good enough.
How to submit your work:                
In Canvas, check in the
file
results.cc
to the assignment “Lab03”.
It’s due 11:59ᴘᴍ MT Saturday, with a 24-hour late period for a 25% penalty.
                
How to receive negative points:                
Turn in someone else’s work.