C++ The Standard Template Library (STL) Quiz
40 in-depth questions covering C++ STL fundamentals including std::vector, std::map, std::unordered_map, std::pair, std::tuple, std::set, and best practices for containers — with 16 code examples to master STL usage and pitfalls.
Question 1
What is the primary purpose of the C++ Standard Template Library (STL)?
Question 2
What is the main difference between std::vector and std::array?
Question 3
What is the difference between std::map and std::unordered_map?
#include <map>
#include <unordered_map>
#include <iostream>
int main() {
std::map<int, std::string> ordered;
std::unordered_map<int, std::string> unordered;
ordered[3] = "three";
ordered[1] = "one";
ordered[2] = "two";
unordered[3] = "three";
unordered[1] = "one";
unordered[2] = "two";
std::cout << "Map (ordered): ";
for (const auto& p : ordered) std::cout << p.first << " ";
std::cout << "\nUnordered map: ";
for (const auto& p : unordered) std::cout << p.first << " ";
return 0;
}Question 4
What is the purpose of std::pair and when should it be used?
Question 5
What is std::tuple and how does it differ from std::pair?
#include <tuple>
#include <iostream>
int main() {
std::pair<int, std::string> p = {42, "hello"};
std::tuple<int, std::string, double> t = {42, "hello", 3.14};
std::cout << std::get<0>(t) << " "
<< std::get<1>(t) << " "
<< std::get<2>(t) << std::endl;
return 0;
}Question 6
What is the difference between std::set and std::unordered_set?
Question 7
What is vector capacity and why does it matter for performance?
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec;
std::cout << "Size: " << vec.size()
<< ", Capacity: " << vec.capacity() << std::endl;
for(int i = 0; i < 100; ++i) {
vec.push_back(i);
if (i % 20 == 0) {
std::cout << "Size: " << vec.size()
<< ", Capacity: " << vec.capacity() << std::endl;
}
}
return 0;
}Question 8
What is the difference between std::vector::push_back and std::vector::emplace_back?
#include <vector>
#include <string>
#include <iostream>
class Item {
public:
std::string name;
int value;
Item(std::string n, int v) : name(n), value(v) {
std::cout << "Constructor called\n";
}
};
int main() {
std::vector<Item> vec;
// push_back creates temporary then moves/copies
vec.push_back(Item("temp", 42));
// emplace_back constructs in-place
vec.emplace_back("direct", 24);
return 0;
}Question 9
What is iterator invalidation and why is it important in STL containers?
Question 10
What is the difference between std::map::operator[] and std::map::at()?
#include <map>
#include <iostream>
int main() {
std::map<std::string, int> scores;
try {
// at() throws exception for non-existent key
int value = scores.at("nonexistent");
} catch (const std::out_of_range& e) {
std::cout << "Exception caught\n";
}
// operator[] creates default value for non-existent key
int defaulted = scores["newkey"]; // Creates scores["newkey"] = 0
return 0;
}Question 11
What is structured binding and how does it work with STL containers?
#include <map>
#include <tuple>
#include <iostream>
int main() {
std::map<std::string, int> scores = {{"Alice", 95}, {"Bob", 87}};
// Structured binding with map
for (const auto& [name, score] : scores) {
std::cout << name << ": " << score << std::endl;
}
// Structured binding with tuple
std::tuple<int, std::string, double> data = {42, "hello", 3.14};
auto [id, text, value] = data;
return 0;
}Question 12
What is the difference between std::vector::reserve and std::vector::resize?
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec;
vec.reserve(10); // Allocates space for 10 elements
std::cout << "After reserve: size=" << vec.size()
<< ", capacity=" << vec.capacity() << std::endl;
vec.resize(5); // Changes size to 5, default-initializes elements
std::cout << "After resize: size=" << vec.size()
<< ", capacity=" << vec.capacity() << std::endl;
return 0;
}Question 13
What is the purpose of std::make_pair and std::make_tuple?
Question 14
What is the difference between std::set::insert and std::set::emplace?
#include <set>
#include <string>
#include <iostream>
class Item {
public:
std::string name;
Item(std::string n) : name(n) {
std::cout << "Constructor: " << name << std::endl;
}
};
int main() {
std::set<Item> items;
// insert requires constructed object
Item temp("temp");
items.insert(temp);
// emplace constructs in-place
items.emplace("direct");
return 0;
}Question 15
What is the difference between std::map::find and std::map::count?
Question 16
What is the difference between std::vector::begin() and std::vector::cbegin()?
Question 17
What is the purpose of std::tie and how does it relate to structured bindings?
#include <tuple>
#include <iostream>
int main() {
std::tuple<int, std::string, double> data = {42, "hello", 3.14};
// Using tie (pre-C++17)
int id;
std::string text;
double value;
std::tie(id, text, value) = data;
// Using structured bindings (C++17+)
auto [new_id, new_text, new_value] = data;
return 0;
}Question 18
What is the difference between std::vector::erase and std::vector::clear?
Question 19
What is the difference between std::set and std::multiset?
Question 20
What is the difference between std::map::lower_bound and std::map::upper_bound?
#include <map>
#include <iostream>
int main() {
std::map<int, std::string> data = {{1, "one"}, {3, "three"}, {5, "five"}};
// lower_bound finds first element >= key
auto lb = data.lower_bound(3);
std::cout << "lower_bound(3): " << lb->first << std::endl;
// upper_bound finds first element > key
auto ub = data.upper_bound(3);
std::cout << "upper_bound(3): " << ub->first << std::endl;
return 0;
}Question 21
What is the difference between std::vector::front() and std::vector::at(0)?
Question 22
What is the difference between std::tuple_cat and std::make_tuple?
#include <tuple>
#include <iostream>
int main() {
std::tuple<int, std::string> t1 = {1, "hello"};
std::tuple<double, char> t2 = {3.14, 'x'};
// make_tuple creates new tuple
auto t3 = std::make_tuple(42, "world");
// tuple_cat concatenates existing tuples
auto t4 = std::tuple_cat(t1, t2);
std::cout << std::get<0>(t4) << " "
<< std::get<1>(t4) << " "
<< std::get<2>(t4) << " "
<< std::get<3>(t4) << std::endl;
return 0;
}Question 23
What is the difference between std::vector::shrink_to_fit and std::vector::clear?
Question 24
What is the difference between std::set::equal_range and std::set::find?
Question 25
What is the difference between std::map::emplace and std::map::insert?
#include <map>
#include <string>
#include <iostream>
class Item {
public:
std::string name;
int value;
Item(std::string n, int v) : name(n), value(v) {
std::cout << "Constructor called\n";
}
};
int main() {
std::map<std::string, Item> items;
// insert requires constructed object
Item temp("temp", 42);
items.insert({"key1", temp});
// emplace constructs in-place
items.emplace("key2", "direct", 24);
return 0;
}Question 26
What is the difference between std::vector::data() and std::vector::begin()?
Question 27
What is the difference between std::pair::first and std::pair::second?
Question 28
What is the difference between std::tuple_size and std::tuple_element?
Question 29
What is the difference between std::vector::max_size and std::vector::capacity?
Question 30
What is the difference between std::set::key_comp and std::set::value_comp?
Question 31
What is the difference between std::map::key_type and std::map::mapped_type?
Question 32
What is the difference between std::vector::const_iterator and std::vector::iterator?
Question 33
What is the difference between std::tuple::get<I> and std::get<I>?
Question 34
What is the difference between std::set::begin() and std::set::rbegin()?
Question 35
What is the difference between std::vector::empty() and std::vector::size() == 0?
Question 36
What is the difference between std::map::cbegin() and std::map::begin()?
Question 37
What is the difference between std::pair::make_pair and std::make_pair?
Question 38
What is the difference between std::vector::back() and std::vector::at(size()-1)?
Question 39
What is the difference between std::set::cend() and std::set::end()?
Question 40
What is the fundamental principle for choosing between STL containers for a given use case?
