Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Vectors
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 36 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++IntermediateLesson 36 of 50CamboFreelanceJune 19, 2026

C++ Vectors

Master C++ vectors: dynamic arrays that grow and shrink. Learn push_back, pop_back, insert, erase, and iteration patterns.


Tutorials › C++ › Vectors
Intermediate8 min readLesson 36 of 50

What is a Vector?

A vector is a dynamic array that can grow or shrink at runtime. It is the most commonly used STL container.

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

int main() {
    vector<int> scores;

    scores.push_back(90);
    scores.push_back(85);
    scores.push_back(78);
    scores.push_back(92);

    cout << "Size: "     << scores.size()  << endl;
    cout << "First: "    << scores.front() << endl;
    cout << "Last: "     << scores.back()  << endl;
    cout << "Index 2: "  << scores[2]      << endl;

    scores.pop_back();   // removes last
    cout << "After pop: " << scores.size() << endl;

    for (int s : scores) cout << s << " ";
    cout << endl;
    return 0;
}

Output:

Size: 4
First: 90
Last: 92
Index 2: 78
After pop: 3
90 85 78

Key Vector Operations

MethodDescription
push_back(val)Add to end
pop_back()Remove from end
insert(pos, val)Insert at iterator position
erase(pos)Remove at iterator position
size()Number of elements
empty()True if size == 0
clear()Remove all elements
at(i)Bounds-checked access
vector<string> fruits = {"Apple", "Banana", "Cherry"};
fruits.insert(fruits.begin() + 1, "Blueberry");
fruits.erase(fruits.begin());   // removes "Apple"
for (const string& f : fruits) cout << f << " ";
// Blueberry Banana Cherry

Exercise

Create a vector of integers. Read numbers from the user until they enter 0. Then print the average of all entered numbers (excluding 0).

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

int main() {
    vector<int> nums;
    int n;
    cout << "Enter numbers (0 to stop): " << endl;
    while (cin >> n && n != 0) nums.push_back(n);

    if (nums.empty()) { cout << "No numbers entered." << endl; return 0; }

    double sum = 0;
    for (int x : nums) sum += x;
    cout << "Average: " << sum / nums.size() << endl;
    return 0;
}

Quiz

  1. Which method adds an element to the end of a vector?

    • A) add()
    • B> append()
    • C) push_back()
    • D> insert_end()
    Answer

    C) push_back()

Summary

  • vector is a dynamic array that resizes automatically.
  • Key operations: push_back, pop_back, insert, erase, size, clear.
  • Prefer at(i) over [i] for bounds-checked access.
← Previous: STL Introduction Next: Maps →
c++vectorsstlintermediate
PreviousLesson 35: C++ STL IntroductionNextLesson 37: C++ Maps
Back to All Tutorials