</>
Skip to content
C++ lessons (36/42)

C++ — Vectors

Create vector

vector<int> nums = {1, 2, 3, 4, 5};

Add elements

nums.push_back(6);
nums.emplace_back(7);

Access elements

cout << nums[0] << endl;
cout << nums.at(1) << endl;

Size

cout << nums.size() << endl;

Iterate

for (int n : nums) {
    cout << n << " ";
}

Mini Practice

  1. Create vector
  2. Add elements
  3. Access elements
  4. Iterate through vector

Up Next

Continue with Maps - Map container.

Related Topics

Frequently Asked Questions about Vectors

What is Vectors in C++?

Vectors is a fundamental concept in C++. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Vectors?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Vectors.

Why is Vectors important in C++?

Vectors is essential for C++ development. Understanding this concept will help you write better code and solve real-world problems more effectively.