FAQ: Vectors - Operations

You can write a for loop that iterates from 0 to vector.size() . And here’s the cool part: you can use the counter of the for loop as the index! Woah.

What does it mean by you can use counter of the for loop as the index

Can someone explain this

Vectors

Operations

So what happens when you want to change each of the values within a vector?

You can use a for loop!

For example, suppose we have an int vector that looks like this:

1

You can write a for loop that iterates from 0 to vector.size() . And here’s the cool part: you can use the counter of the for loop as the index! Woah.

for (int i = 0; i < vector.size(); i++) { vector[i] = vector[i] + 10; }

This will change the vector to:

2

Here, we incremented i from 0 to vector.size() , which is 3. During each iteration, we are adding 10 to the element at position i :

  • When i = 0 , we added 10 to vector[0]
  • When i = 1 , we added 10 to vector[1]
  • When i = 2 , we added 10 to vector[2]

Hi everyone!

I have an issue with this problem/explanation. In the box below we see the solution which is: total = total + delivery_order[i];

Why exactly are we using brackets to include the variable i? From my understanding this goes against what we learned in previous chapters. An index refers to a element’s position within an ordered list.

So why do we use a Vector index notation for a variable that is not in the list?

Thanks!

Hello, @some1dude, and welcome to the forums.

Also, hello, @krish2882005. If you are still needing an explanation, perhaps this will help.

The variable i in the example is initialized with a value of 0, and incremented by 1 with each iteration of the for loop until i < delivery_order.size(); is no longer true. So, when i is equal to the size of the vector, control flow of the program proceeds to the next line of code after the for loop’s closing }. i is only used as the value of the index, so we can access each element of the vector. Here’s another example:

#include <iostream>
#include <vector>

int main() {
  std::vector<std::string> hello = {"Hello ", "there, ", "some1dude!"};

  for(int i = 0; i < hello.size(); i++) { //hello.size() is 3, so i starts at 0, and the loop is exited when i gets to 3 since 3 is not less than 3
    std::cout << hello[i]; // this code will execute 3 times once for each value: 0, 1, 2
    //The output will be hello[0] followed by hello[1] and hello[2] as shown below
  }
  //when i gets to 3, the for loop is finished, and the line below will be executed next
  std::cout << "\nThe for loop has finished.";
}

Output:

Hello there, some1dude!
The for loop has finished.

1 Like

Hello,

Thank you for the reply it was really helpful!
I ran the code however I did notice that the output for the for loop skips hello[i].
It is not displaying “hello 0”, “hello 1”, “hello 2”.It seems that it should display that since [i] next to the hello is also being incremented. Did I miss something because I get the feeling that it should display hello along with the numbers?

Thanks

Thank you that also solves the doubt

1 Like

The output is:

Hello there, some1dude!

The elements of the vector make up the output. The vector contains 3 elements. Each is a string literal: { "Hello ", "there ", "some1dude" }.

This line:

std::cout << hello[i];

prints each element of the vector (one at a time) on the same line.
hello[0] is the element of the vector at index 0 which is "Hello "
hello[1] is the element of the vector at index 1 which is "there, "
hello[2] is the element of the vector at index 2 which is "some1dude!"

When you print all three in succession on the same line, you get:

Hello there, some1dude!

2 Likes

Okay I’m sorry about that! I confused the name of the vector with the results. Everything makes sense to me now! Thank you for your time and patience.

1 Like

My confusion is with the part in the curly braces of the for loop:

for (int x = 0; x < delivery_order.size(); x++)
{total = total + delivery_order[x];}

Why isn’t it just total+delivery_order?
Why do we include the total=?

Wouldn’t that just reset total’s value each loop?

thank you! I didnt fully understand what was really happening inside the code, this explained it so well!

1 Like