• Home
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • React
      • Introduction to React
      • React Developer
    • Python
      • Introduction to Python
      • Python Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Reading Grounds

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds
  • Featured on DanielLaunches

Courses

  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

C++ Developer -

Lesson 1 of 46

Lesson Progress: 0%

Lesson Progress: 0%
Lesson Incomplete
Lesson 1 of 46
Next: References →

The vector is a dynamic array from the C++ Standard Library. It can grow and shrink at runtime while providing contiguous storage like a regular array. Vectors are declared with #include <vector> and are one of the most commonly used containers. This lesson covers creating vectors, adding elements with push_back, accessing items with brackets and at(), iterating with loops, modifying elements, and removing the last element. Understanding vectors is essential for writing flexible and efficient C++ programs.

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

int main() {
    vector<int> nums;
    nums.push_back(10);
    nums.push_back(20);
    nums.push_back(30);
    cout << nums.size();
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • vector<int> declares a vector that holds integers.
  • push_back() appends a new element to the end of the vector.
  • size() returns how many elements are currently stored.
  • Vectors grow automatically as elements are added.
Code Example
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {10, 20, 30};
    cout << nums[0] << " ";
    cout << nums.at(2);
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Initializer list syntax with curly braces creates a vector with the given elements.
  • Access elements using vector[index] or the safer vector.at(index).
  • The at() method performs bounds checking and throws an exception if the index is invalid.
  • Indices start at 0, just like arrays.
Code Example
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {5, 10, 15};
    for (int i = 0; i < nums.size(); i++) {
        if (i > 0) cout << " ";
        cout << nums[i];
    }
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A for loop with size() iterates over all elements by index.
  • The condition i < nums.size() ensures you stay within bounds.
  • This pattern works for any sequential container that supports indexing.
  • Output is formatted with spaces between elements using the if (i > 0) check.
Code Example
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {1, 2, 3};
    nums[1] = 99;
    for (int i = 0; i < nums.size(); i++) {
        if (i > 0) cout << " ";
        cout << nums[i];
    }
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • You can modify an existing element using assignment with bracket notation.
  • nums[1] = 99 changes the second element without changing the size.
  • Modifying an element is O(1) because vectors provide direct access.
  • After modification, iterating shows the updated values.
Code Example
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {1, 2, 3};
    cout << nums.size() << " " << nums[1];
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • size() reflects the current number of elements.
  • Accessing a valid index with operator[] retrieves the stored value.
  • You can combine multiple vector operations in a single output statement.
  • Always ensure the index is valid before using operator[].
Code Example
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {10, 20, 30};
    nums.pop_back();
    cout << nums.size() << " " << nums[1];
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • pop_back() removes the last element and reduces size by one.
  • Calling pop_back() on an empty vector causes undefined behaviour.
  • After removal, the remaining elements are still accessible by index.
  • Use empty() to check before calling pop_back() safely.

Test Incomplete

What does push_back() do to a vector?

Question #

1/15

Score

0/0 - 0.0 %