Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C++ Vectors: Dynamic Arrays, Iterators, and Practical Examples

What is a C++ Vector?

A C++ vector is a dynamic array that automatically resizes itself as elements are added or removed. Its elements are stored contiguously, enabling fast indexed access and traversal via iterators.

Insertion occurs at the end in amortized constant time. Removing the last element is also constant time, while inserting or erasing at the beginning or middle requires shifting elements, resulting in linear time complexity.

When to Use a Vector?

How to Initialize Vectors in C++

std::vector<data‑type> name (size, value); // size and value are optional

Iterators

Iterators provide a pointer‑like interface to vector elements. Common iterator functions include:

Modifiers

Modifiers change the vector’s contents. Key functions are:

Example 1: Basic Iteration

#include <iostream>
#include <vector>

int main() {
    std::vector<int> nums;
    for (int a = 1; a <= 5; ++a) {
        nums.push_back(a);
    }

    std::cout << "Output from begin and end: ";
    for (auto it = nums.begin(); it != nums.end(); ++it) {
        std::cout << *it << ' ';
    }

    std::cout << "\nOutput from cbegin and cend: ";
    for (auto it = nums.cbegin(); it != nums.cend(); ++it) {
        std::cout << *it << ' ';
    }
    return 0;
}

Output:

Mastering C++ Vectors: Dynamic Arrays, Iterators, and Practical Examples

Example 2: Advanced Modifiers

#include <iostream>
#include <vector>

int main() {
    std::vector<int> nums;
    nums.assign(5, 1); // five elements, all initialized to 1

    std::cout << "Vector contents: ";
    for (size_t i = 0; i < nums.size(); ++i) {
        std::cout << nums[i] << ' ';
    }

    nums.push_back(2);
    std::cout << "\nLast element: " << nums.back();

    nums.pop_back();
    std::cout << "\nVector contents: ";
    for (int a : nums) std::cout << a << ' ';

    nums.insert(nums.begin(), 7);
    std::cout << "\nFirst element: " << nums.front();

    nums.clear();
    std::cout << "\nSize after clear(): " << nums.size();
    return 0;
}

Output:

Mastering C++ Vectors: Dynamic Arrays, Iterators, and Practical Examples

Capacity Functions

Determine and manipulate a vector’s storage characteristics with the following functions:

Example 3: Capacity Exploration

#include <iostream>
#include <vector>

int main() {
    std::vector<int> vector1;
    for (int x = 1; x <= 10; ++x) {
        vector1.push_back(x);
    }

    std::cout << "Vector size: " << vector1.size() << std::endl;
    std::cout << "Vector capacity: " << vector1.capacity() << std::endl;
    std::cout << "Maximum size of vector: " << vector1.max_size() << std::endl;

    vector1.resize(5);
    std::cout << "Vector size after resizing: " << vector1.size() << std::endl;

    if (!vector1.empty())
        std::cout << "Vector is not empty" << std::endl;
    else
        std::cout << "Vector is empty" << std::endl;

    return 0;
}

Output:

Mastering C++ Vectors: Dynamic Arrays, Iterators, and Practical Examples

Key Takeaways

C Language

  1. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  2. Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples
  3. Master C++ Dynamic Array Allocation: A Practical Guide with Code Examples
  4. Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
  5. C++ Structs Explained with a Practical Example
  6. Mastering std::map in C++: Comprehensive Guide with Code Examples
  7. C++ Classes & Objects: A Practical Guide with Code Examples
  8. C++ Polymorphism Explained: Practical Examples & Key Concepts
  9. Mastering std::list in C++: Syntax, Functions & Practical Examples
  10. Dynamic Memory Allocation in C: Understanding malloc() with Practical Examples