C++ Standard Library Essentials: Exploring Strings, I/O, and Containers - Biz Tech

C++ Standard Library Essentials: Exploring Strings, I/O, and Containers

Listen
#include <iostream>
#include <string>
#include <vector>
#include 
<map>
#include <algorithm>

int main() {
    // String manipulation
    std::string str1 = "Hello";
    std::string str2 = "World";
    std::string combinedString = str1 + " " + str2;

    std::cout << "Combined string: " << combinedString << std::endl;

    // I/O using getline
    std::string userInput;
    std::cout << "Please enter a string: ";
    std::getline(std::cin, userInput);
    std::cout << "You entered: " << userInput << std::endl;

    // Containers: Vector
    std::vector<int> numbers = {4, 7, 1, 9, 3};
    std::sort(numbers.begin(), numbers.end());

    std::cout << "Sorted numbers: ";
    for (int num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // Containers: Map
    std::map<std::string, int> ages;
    ages["Alice"] = 30;
    ages["Bob"] = 25;
    ages["Charlie"] = 22;

    std::cout << "Ages:" << std::endl;
    for (const auto& pair : ages) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }

    return 0;
}
  

This code block demonstrates the essentials of the C++ Standard Library, exploring strings, I/O, and containers. The program shows string manipulation and concatenation, using getline for input, sorting a vector, and iterating through a map.