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

Building a New Vector

Sometimes you want to know not only how many elements meet a condition, but also which ones. Then start with an empty vector and add each matching element to it with push_back():

std::vector<int> temperatures = {18, 24, 27, 15, 31, 22, 29};
std::vector<int> hot = {};
for (int temperature : temperatures) {
    if (temperature >= 25) {
        hot.push_back(temperature);
    }
}

std::cout << "Hot days: " << hot.size() << std::endl;
for (int temperature : hot) {
    std::cout << temperature << " ";
}
std::cout << std::endl;
Hot days: 3
27 31 29 

std::vector<int> hot = {}; creates a vector with no elements. You can also write just std::vector<int> hot;: unlike an int without a value, a vector without a value doesn't hold garbage, it's empty. Each push_back() adds one element at the end, so the new vector keeps the order of the original one. The original vector stays as it was, and hot.size() tells you how many elements you collected. To print the new vector, you need another loop, because std::cout << hot doesn't work.

The new vector doesn't have to hold the same values. This loop builds a vector with every number doubled:

std::vector<int> numbers = {3, 8, 5};
std::vector<int> doubled = {};
for (int number : numbers) {
    doubled.push_back(number * 2);
}

for (int number : doubled) {
    std::cout << number << " ";
}
std::cout << std::endl;
6 16 10 

Create the new vector before the loop, just like a sum or a counter. Inside the loop, every iteration would start with a new empty vector, and after the loop, the vector wouldn't exist at all.

Instructions

  1. 1

    Create an empty vector evenNumbers of type std::vector<int>. Then go through numbers and add every even number to evenNumbers with push_back(). A number is even when number % 2 == 0.

  2. 2

    After that loop, print the elements of evenNumbers on one line, after the text Even: and separated by spaces, the same way the program prints all the numbers. The output is Even: 12 30 18 4.

  3. 3

    On the next line, print how many even numbers there are, with evenNumbers.size(): Count: 4.

Back

9/13

  1. 1. The while Loop
  2. 2. Infinite Loops
  3. 3. The for Loop
  4. 4. Looping over a Vector
  5. 5. Loops and Text
  6. 6. Sum and Average
  7. 7. Counting
  8. 8. The Largest Value
  9. 9. Building a New Vector
  10. 10. The break Statement
  11. 11. The continue Statement
  12. 12. Nested Loops
  13. 13. Summary
Next

Try it yourself

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