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

Summary

In this lesson, you learned that:

  • A vector stores several values of the same type, its elements, in one variable, in a fixed order. It needs #include <vector>. The type of the elements goes in angle brackets: std::vector<int> scores = {85, 92, 78}; or std::vector<std::string> names = {"Anna", "Tom"};. std::vector<int> points; is an empty vector.
  • Each element has an index, counted from 0. names[0] reads the first element, names[1] = "Lucy"; changes the second one. An element behaves like a variable of its type, so scores[0] += 1; works too.
  • You can't print a whole vector with std::cout. Print it element by element.
  • A method is a command that belongs to a value. It's written after a dot and is always followed by parentheses. names.size() gives you the number of elements. The last element is at index names.size() - 1.
  • Square brackets don't check the index: an index outside the vector reads a meaningless value, or the program crashes. names.at(5) checks the index, and when it's outside the vector, it stops the program with an out_of_range error.
  • push_back() adds an element to the end of a vector. pop_back() removes the last element and doesn't give anything back, so read an element you need before removing it.
  • A vector can contain other vectors, called nested vectors: std::vector<std::vector<std::string>> teams. teams[1][0] is the first element of the second inner vector.
  • A std::string text is a sequence of characters of type char. You read a character with text[0] or text.at(0) and get the number of characters with text.size() or text.length(). A character is written in single quotes: 'A'. Letters with accents take up two chars, so write the texts you work with character by character without accents.

In the next lesson, on loops, you'll learn to repeat code, for example to go through all the elements of a vector one by one, however many there are. But first, practice what you know on the readings of a thermometer.

Instructions

  1. 1

    The last reading, 99, is a thermometer error. Remove it from temperatures with pop_back(), and then add the correct value 25 to the end of the vector with push_back().

  2. 2

    Print the first temperature, the last temperature and the number of readings, so that the output includes the line First: 18, last: 25, readings: 5. Find the last temperature with size() - 1.

  3. 3

    Below that, write a condition: if the last temperature is higher than the first one, print Getting warmer, otherwise print Getting colder. (Once this step passes, try changing the correct value from 25 to 15 and see what the program prints.)

Back

10/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.