Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Creating a Vector

In the lesson on conditions, you learned how a program makes decisions. In this lesson, you'll learn to store several values together.

Imagine you want to store your scores from five tests. With what you know so far, you would need five variables:

int score1 = 85;
int score2 = 92;
int score3 = 78;
int score4 = 90;
int score5 = 88;

That's a lot of typing. And what if there's a sixth test? You would have to create a new variable for every new score. That's what a vector is for. A vector stores several values of the same type in one variable:

std::vector<int> scores = {85, 92, 78, 90, 88};

The values in a vector are called its elements. This vector has five elements, and their order matters: 85 is the first element, 88 the last one.

Let's go through it from left to right:

  • std::vector says that you're creating a vector. It comes from the standard library, just like std::cout and std::string, so it starts with std::.
  • The angle brackets < > hold the data type of the elements. std::vector<int> is a vector of whole numbers, std::vector<double> a vector of decimal numbers and std::vector<std::string> a vector of texts.
  • scores is the name of the variable. The same rules apply to it as to the names of other variables.
  • The curly braces { } hold the elements, separated by commas.

To use vectors, write #include <vector> at the top of your program, just like #include <string> for texts. Sometimes a program works without it, because another #include brings vectors in. Don't rely on that and always write it.

More examples:

std::vector<std::string> names = {"Jennifer", "Boris", "Nancy"};
std::vector<double> prices = {29.9, 149.9, 99.9};
std::vector<int> points;

The vector points has no elements, it's empty. Unlike an int variable without a starting value, it doesn't hold any random values. There's simply nothing in it yet. You'll learn to add elements to a vector later in this lesson.

All elements must have the type written in the angle brackets. If you put a decimal number into a vector of whole numbers, the program doesn't compile:

std::vector<int> scores = {85, 92.5, 78};
main.cpp:6:36: error: type 'double' cannot be narrowed to 'value_type' (aka 'int') in initializer list [-Wc++11-narrowing]

The message says that a double value can't be narrowed to the type of the vector's elements (value_type), which is int. The decimal part would be lost, so C++ reports an error instead. You can ignore the name in square brackets at the end of the message.

You can't print a whole vector at once, only element by element. To print an element, write its number in square brackets after the vector's name. names[0] is the first element of the vector names:

std::cout << names[0] << std::endl;

This prints:

Jennifer

You'll find out why counting starts at zero in the next exercise. The program in the editor prints the first colleague the same way.

Instructions

  1. 1

    Below the comment, create a vector of whole numbers named scores with the elements 85, 92, 78 and 90, in this order.

  2. 2

    Below the vector, print the first score with a label, just like the program prints the first colleague. The output should include the line First score: 85.

Back

1/10

  1. 1. Creating a Vector
  2. 2. Reading Elements
  3. 3. Changing Elements
  4. 4. Size and the Last Element
  5. 5. Index Out of Range
  6. 6. Adding Elements: push_back()
  7. 7. Removing Elements: pop_back()
  8. 8. Nested Vectors
  9. 9. Characters of a Text
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.