CS253: Software Development with C++

Spring 2023

STL Containers

Show Lecture.STLContainers as a slide show.

CS253 STL Containers

Overview

Attributes

NameOrderSearchInsertAppend
string, vectornoO(n )O(n )O(1)
dequenoO(n )O(n )O(1)
list, forward_listnoO(n )O(1)O(1)
[multi]set<O(log n )O(log n ) 
[multi]map<O(log n )O(log n ) 
unordered_[multi]sethashedO(1)O(1) 
unordered_[multi]maphashedO(1)O(1) 

Popular methods

Popular types

string

string con = "The quick brown fox jumps over a lazy dog.";
cout << "size=" << con.size() << ' ';
for (auto c : con)
    cout << c;
size=42 The quick brown fox jumps over a lazy dog.

vector

string s = "The quick brown fox jumps over a lazy dog.";
vector<char> con(s.begin(), s.end());
cout << "size=" << con.size() << ' ';
for (auto c : con)
    cout << c;
size=42 The quick brown fox jumps over a lazy dog.

We can’t initialize a vector directly from a C-style string, so we put it into a C++ string, and initialize the vector from that, using the two-iterator constructor.

array

string s = "The quick brown fox jumps over a lazy dog.";
array<char, 42> con;
for (size_t i=0; i<s.size(); i++)
    con[i] = s[i];
cout << "size=" << con.size() << ' ';
for (auto c : con)
    cout << c;
size=42 The quick brown fox jumps over a lazy dog.

list

string s = "The quick brown fox jumps over a lazy dog.";
list<char> con(s.begin(), s.end());
cout << "size=" << con.size() << ' ';
for (auto c : con)
    cout << c;
size=42 The quick brown fox jumps over a lazy dog.

forward_list

string s = "The quick brown fox jumps over a lazy dog.";
forward_list<char> con(s.begin(), s.end());
for (auto c : con)
    cout << c;
The quick brown fox jumps over a lazy dog.

deque

string s = "The quick brown fox jumps over a lazy dog.";
deque<char> con(s.begin(), s.end());
cout << "size=" << con.size() << ' ';
for (auto c : con)
    cout << c;
size=42 The quick brown fox jumps over a lazy dog.

set

string s = "The quick brown fox jumps over a lazy dog.";
set<char> con(s.begin(), s.end());
cout << "size=" << con.size() << ' ';
for (auto c : con)
    cout << c;
size=28  .Tabcdefghijklmnopqrsuvwxyz

multiset

string s = "The quick brown fox jumps over a lazy dog.";
multiset<char> con(s.begin(), s.end());
cout << "size=" << con.size() << ' ';
for (auto c : con)
    cout << c;
size=42         .Taabcdeefghijklmnoooopqrrsuuvwxyz

A multiset is like a set, but allows copies.

unordered_set

string s = "The quick brown fox jumps over a lazy dog.";
unordered_set<char> con(s.begin(), s.end());
cout << "size=" << con.size() << ' ';
for (auto c : con)
    cout << c;
size=28 gdyzlavspmjxfn.huerTqickbo w

unordered_multiset

string s = "The quick brown fox jumps over a lazy dog.";
unordered_multiset<char> con(s.begin(), s.end());
cout << "size=" << con.size() << ' ';
for (auto c : con)
    cout << c;
size=42 .gdyzlaaspmjxfnwoooorrbkciuuqv        eehT

A unordered_multiset (formerly hash_multiset), is a hash table implementation like a multiset.

map, multimap, unordered_map, unordered_multimap

map example

map<string, double> gpa = {
    { "Logan",  3.998 },
    { "Russ",  4.20  },
    { "Elisa", 3.92  },
    { "Craig", 2.0   }
};

for (auto p : gpa)
    cout << p.first << " has a GPA of " << p.second << '\n';
cout << "Logan’s GPA is: " << gpa["Logan"] << '\n';
Craig has a GPA of 2
Elisa has a GPA of 3.92
Logan has a GPA of 3.998
Russ has a GPA of 4.2
Logan’s GPA is: 3.998