C++ and Object-Oriented Programming

C++ Standard Template Library (STL); Namespaces

C-CAT

C++ Standard Template Library (STL)

What is STL?

The Standard Template Library (STL) is a collection of template classes and functions providing common data structures and algorithms.

Components:

  1. Containers — data storage (vector, list, map, set, etc.)

Algorithms — operations (sort, find, reverse, etc.) 3. Iterators — pointer-like objects to traverse containers

19.1 Vector

#include <vector>
#include <iostream>
using namespace std;

int main() {
    // Dynamic array
    vector<int> v = {5, 2, 8, 1, 9};

    // Add elements
    v.push_back(7);      // [5,2,8,1,9,7]
    v.push_back(3);

    // Access
    cout << v[0] << endl;         // 5
    cout << v.at(2) << endl;      // 8 (with bounds checking)
    cout << v.front() << endl;    // 5
    cout << v.back() << endl;     // 3

    // Size
    cout << "Size: " << v.size() << endl;

    // Iterate
    for (int x : v) cout << x << " ";  // range-based for
    cout << endl;

    // Sort
    sort(v.begin(), v.end());
    for (int x : v) cout << x << " ";  // 1 2 3 5 7 8 9

    // Remove last
    v.pop_back();

    // Find & erase
    auto it = find(v.begin(), v.end(), 5);
    if (it != v.end()) v.erase(it);

    // 2D vector (matrix)
    vector<vector<int>> matrix(3, vector<int>(3, 0));
    matrix[1][2] = 42;

    return 0;
}

19.2 string

#include <string>
using namespace std;

string s = "Hello, World";
cout << s.length() << endl;           // 12
cout << s.substr(7, 5) << endl;       // World
cout << s.find("World") << endl;      // 7
s.replace(7, 5, "C++");               // Hello, C++
cout << (s == "Hello, C++") << endl;  // 1 (true)
s += "!";
cout << s << endl;                    // Hello, C++!

19.3 map

#include <map>
using namespace std;

map<string, int> wordCount;

// Insert
wordCount["hello"] = 1;
wordCount["world"] = 2;
wordCount["hello"]++;   //
wordCount["hello"] = 2

// Access
cout << wordCount["hello"] << endl;  // 2
cout << wordCount.count("world") <<
endl;  // 1 (exists), 0 (not)

// Iterate
for (auto &pair : wordCount) {
    cout << pair.first << ": " << pair.second <<
endl;
}
// Keys are sorted alphabetically in map

// Find
auto it = wordCount.find("hello");
if (it != wordCount.end()) cout << "Found: " << it->second;

19.4 unordered_map

#include <unordered_map>
// O(1) average access (hash table internally)
unordered_map<string, int> umap;
umap["key"] = 42;

19.5 set and multiset

#include <set>
set<int> s = {5, 2, 8, 2, 1, 5};  // {1, 2, 5, 8} — sorted, no duplicates
s.insert(3);                         // {1, 2, 3, 5, 8}
s.erase(2);                          // {1, 3, 5, 8}
cout << s.count(5) << endl;          // 1 (exists)

19.6 STL Algorithms

#include <algorithm>
#include <vector>

vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6};

sort(v.begin(), v.end());                         // ascending sort
sort(v.begin(), v.end(),
greater<int>());         // descending sort

auto it = find(v.begin(), v.end(), 4);            // find element
bool found =
binary_search(v.begin(), v.end(), 4); // binary search (sorted)

int total = accumulate(v.begin(), v.end(), 0);    // sum
int maxVal =
*max_element(v.begin(), v.end());    // maximum
int minVal = *min_element(v.begin(),
v.end());    // minimum

reverse(v.begin(), v.end());                       // reverse
count(v.begin(), v.end(), 1);
// count occurrences

remove_if(v.begin(), v.end(), [](int x){ return x < 3; }); // remove <3

Namespaces

What is a Namespace?

A namespace avoids name conflicts by grouping identifiers under a unique name.

namespace MyProject {
    int value = 42;

    void display() {
        cout << "Value: " << value << endl;
    }

    class Manager {
    public:
        void run() { cout << "Manager running\n"; }
    };
}

// Access namespace members
cout << MyProject::value << endl;    // 42
MyProject::display();
MyProject::Manager mgr;
mgr.run();

// using directive — bring all names into scope
using namespace MyProject;
cout << value <<
endl;    // no prefix needed now

// using declaration — bring specific name
using MyProject::display;
display();

Standard Namespace

// The standard library is in namespace 'std'
std::cout << "Hello\n";
std::cin >> x;
std::vector<int> v;

// OR using namespace std (common in learning)
using namespace std;
cout << "Hello\n";

Continue learning

Related notes

Put this topic into timed practice

Open mock tests when you want full-exam pacing, or keep drilling in practice mode.