Loading workspace environment...
Master dynamic `std::vector`, associative `std::unordered_map`, range-based for loops, and STL algorithms (`std::sort`, `std::transform`).
std::vector<int> vec = {1, 2, 3}; vec.push_back(4);Dynamic resizable array container.std::unordered_map<std::string, int> map;O(1) hash map lookup container.#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
int main() {
std::vector<std::string> words = {"apple", "banana", "apple", "orange", "banana", "apple"};
std::unordered_map<std::string, int> freq;
for (const auto& w : words) {
freq[w]++;
}
std::cout << "Word Frequencies:\n";
for (const auto& [word, count] : freq) { // C++17 structured binding
std::cout << " " << word << ": " << count << "\n";
}
// Sort a numeric vector with lambda predicate
std::vector<int> scores = {85, 92, 78, 99, 88};
std::sort(scores.begin(), scores.end(), std::greater<int>());
std::cout << "Scores sorted descending: ";
for (int s : scores) std::cout << s << " ";
std::cout << "\n";
return 0;
}Word Frequencies:
orange: 1
banana: 2
apple: 3
Scores sorted descending: 99 92 88 85 78for (int i = 0; i <= vec.size(); i++) // Off-by-one out of bounds error!for (const auto& item : vec) // Idiomatic range-based for loopArchitectural Explanation: Manual index loops risk off-by-one errors. Prefer range-based for loops or STL iterators.
Given a vector of integers, use an unordered_map to count frequencies and print the most frequent number.