1

I'm trying to learn C++ vectors.. Here is the code:

#include <iostream>
#include <vector>
using namespace std;
    
int main(){
    
    vector <int> vec;
    
    for(int i=0; i<=10; i++){
        vec.push_back(i);
    }

    for(auto i=vec.begin(); i!=vec.end();i++){
        cout<<*i<<" ";
    }
}

Can anybody tell me what this part is?

for(auto i=vec.begin(); i!=vec.end();i++){
    cout<<*i<<" ";
}  

I've searched the Internet but couldn't find a clear explanation.

Ok, it prints the numbers that we put it in the vector, but can I get a more technical explanation?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    Iterators, can't believe that wherever you learned about vectors didn't mention iterators. They are an extremely important concept in the standard library. – john Jul 22 '20 at 20:30
  • 1
    maybe I should change the tutorial that I'm watching thanks for the answer. – Mücahit Uğurlu Jul 22 '20 at 20:32
  • Sounds like you're trying to learn C++ from YouTube, which is a horrible source. There are too many people who don't know what they're talking about out there. I recommend you switch to a [good book](https://stackoverflow.com/q/388242/9254539). – eesiraed Jul 23 '20 at 01:00

3 Answers3

2
for(auto i=vec.begin(); i!=vec.end();i++){
    cout<<*i<<" ";
}  

This is just the iterators in C++.

  • begin() function is used to return an iterator pointing to the first element of the vector.

  • Similarly, end() function is used to return an iterator pointing past the last element of the vector.

  • auto just deduces the type of the variable i. You could have also specified it as std::vector<int>::iterator i = vec.begin() . That's the type of the iterator you are using to loop over the vector.

  • In the above piece of code, you are basically iterating from the beginning of the vector until the end of the vector.

  • Inside the loop, you are just dereferencing the iterator and printing the value at the current position where the iterator is.

What the above piece of code is doing is basically the same as the following type of loop, which uses indexing to loop over the array:

for(size_t i = 0; i != vec.size() ; i++){
    cout << vec[i] << " ";
}

You should read more about iterators, as they are a core concept in C++. You can read more about them here:

iterators

std::vector.begin()

std::vector.end()

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
2

This is an iterator to the first element in the vector: vec.begin()

An iterator to one-past the last element of the vector: vec.end()

auto deduces the type of i from vec.begin(), its an iterator. We really do not need to care about the exact type.

We only need to know that we can increment it: i++.

And compare two iterators with each other to check if we are at the end: i != vec.end().

And we can derference iterators to get the element the "point to": *i.

Without iterators the loop could be written as:

for (size_t i=0; i<vec.size(); ++i) {
    std::cout << vec[i];
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

That part simply prints all the elements of the vector. auto automatically determines what data structure it is given the parameters that define it. In this case, it is being used as a vector<int>::iterator. Mostly, this is used in other data structures, such as a map or a set, since those don't support random access. In a vector, you can simply do

for(int i = 0; i < vec.size(); i++)
{
    cout << vec[i] << " ";
}
  • 2
    "*auto is like a "wildcard", and can be used as any data structure*" - that is not what `auto` is. What you described is `std::any` instead. `auto` is used for "type deduction" only. The variable being declared still needs a specific type, but the compiler can *deduce* that type based on what type of value is being *assigned* to the variable. That is not a "wildcard data structure" at all. – Remy Lebeau Jul 22 '20 at 20:47
  • Yup, I got a bit confused there. It's edited now. Thanks! –  Jul 22 '20 at 21:04