Creating a Vector
Part of the Logic & Flow section of Coddy's C++ journey — lesson 10 of 56.
Now that you understand what vectors are, let's learn how to actually create them. There are several ways to declare and initialize a std::vector, depending on your needs.
The most basic approach is to create an empty vector that you can fill later:
std::vector<int> numbers;This creates an empty vector called numbers that can hold integers. Notice the angle brackets <int> — this tells the vector what type of data it will store.
You can also initialize a vector with values right from the start using an initializer list:
std::vector<int> scores = {85, 92, 78, 96, 88};This creates a vector with five integers already in it. The curly braces contain the initial values, separated by commas.
Vectors can hold different data types — just change what's inside the angle brackets. For example, std::vector<string> for text or std::vector<double> for decimal numbers. The flexibility of vectors makes them perfect for storing collections of data when you need the convenience of automatic resizing.
Challenge
EasyCreate a program that demonstrates vector initialization by creating a vector of integers with predefined values and displaying its contents.
The following inputs will be provided:
- Five integers that should be used to initialize the vector
Your program should:
- Create a
std::vector<int>namednumbersand initialize it with the five input values using an initializer list - Print each element of the vector on a separate line in the exact format shown below
- After printing all elements, print the total number of elements in the vector
Use the following exact output format:
Element 0: [first value]
Element 1: [second value]
Element 2: [third value]
Element 3: [fourth value]
Element 4: [fifth value]
Vector size: 5Access the vector elements using the square bracket [] operator with indices 0 through 4 to print each value. Use the .size() method to get the number of elements in the vector.
Cheat sheet
To create a std::vector, specify the data type in angle brackets:
std::vector<int> numbers; // Empty vectorInitialize a vector with values using an initializer list:
std::vector<int> scores = {85, 92, 78, 96, 88};Access vector elements using square brackets with indices:
scores[0] // First element
scores[1] // Second elementGet the number of elements using .size():
scores.size() // Returns number of elementsVectors can hold different data types by changing the type in angle brackets:
std::vector<string>for textstd::vector<double>for decimal numbers
Try it yourself
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Read input values
int val1, val2, val3, val4, val5;
cin >> val1 >> val2 >> val3 >> val4 >> val5;
// TODO: Write your code below
// Create a vector named 'numbers' and initialize it with the input values
// Print each element using the required format
// Print the vector size
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers and Memory
What is a Pointer?Address-Of OperatorDereference OperatorNull PointersPointers and ArraysDynamic Memory with 'new'Freeing Memory with 'delete'Recap - Pointer Practice2Vectors (Dynamic Arrays)
Introducing std::vectorCreating a VectorAdding ElementsAccessing ElementsVector SizeIterating with a For LoopRange-Based For LoopRemoving ElementsRecap - Vector Operations5Project: Inventory Tool
Project SetupAdding and Updating Items