-7

Everyone I am new to C++ and just moved from C. While studying vectors I came across this:

    #include <iostream>
    #include <vector>

    using namespace std;

    int main()
    {
        vector <int> g1;
        vector <int> :: iterator i;
        vector <int> :: reverse_iterator ir;

        for (int i = 1; i <= 5; i++)
            g1.push_back(i);

        cout << "Output of begin and end\t:\t";
        for (i = g1.begin(); i != g1.end(); ++i)
            cout << *i << '\t';

        cout << endl << endl;
        cout << "Output of rbegin and rend\t:\t";
        for (ir = g1.rbegin(); ir != g1.rend(); ++ir)
            cout << '\t' << *ir;

        return 0;

    }

My question is why here vector <int> :: iterator i; and is there a difference between vector <int> :: iterator i; and int i?

nimish642
  • 29
  • 8
  • 4
    You should probably [read a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) as that should answer your questions here. – Some programmer dude Aug 03 '17 at 05:35
  • The why is because whomever wrote this code wants to *iterate* over the vector. Or am I missing the question? – StoryTeller - Unslander Monica Aug 03 '17 at 05:35
  • 1
    By the way, coming from C you should already know something about scoping and how you can have multiple variables with the same name in different scopes. – Some programmer dude Aug 03 '17 at 05:36
  • Possible duplicate of [How to navigate through a vector using iterators? (C++)](https://stackoverflow.com/questions/2395275/how-to-navigate-through-a-vector-using-iterators-c) –  Aug 03 '17 at 05:36
  • 3
    The title of this question comes from outer space. Please fix. – n. m. could be an AI Aug 03 '17 at 05:40
  • Your question is mainly about the two variables named `i` but defined using different types. The title of the question has no relation to that. – R Sahu Aug 03 '17 at 05:58

2 Answers2

0

why here vector <int> :: iterator i;

The vector <int> :: iterator i; is created in order to traverse the vector vector <int> g1; (actually it can traverse any vector<int>, but in this case, that's for g1)

is there a difference between vector <int> :: iterator i; and int i?

The scope of vector <int> :: iterator i; is the main function. Inside main, a new scope is created:

for (int i = 1; i <= 5; i++)
        g1.push_back(i);

In that scope, i is the int defined in the loop start, but it dies at the end of the loop, and after that i "returns to be" the vector <int> :: iterator i

int main()
{
    vector <int> g1;
    vector <int> :: iterator i;           // i is the iterator
    vector <int> :: reverse_iterator ir;

    for (int i = 1; i <= 5; i++)         // here i the int
        g1.push_back(i);                 // also here
                                         // from here the int i is dead, and i is back to being an iterator
    cout << "Output of begin and end\t:\t";
    for (i = g1.begin(); i != g1.end(); ++i)  
        cout << *i << '\t';

    cout << endl << endl;
    cout << "Output of rbegin and rend\t:\t";
    for (ir = g1.rbegin(); ir != g1.rend(); ++ir)
        cout << '\t' << *ir;

    return 0;

}
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

There is difference between vanilla C and modern C++ in that the variables declared in for() would have scope and life length of for() loop body. They are ALLOWED to mask variables with same names, declared in upward scopes.

This example pretty much works in same way:

int main()
{

    int i = 5, j;

    for(int i = 5; i< 10; i++)
        j = i;

    std::cout << i << " " << j;

    return 0;
}
// Output: 5 9

The code in your question is inelegant and contains a bad practice.

#include <iostream>
#include <vector>
#include <algorithm> 

int main()
{
    using namespace std; // never do this in global scope

    vector <int> g1(5);  // allocating vector of 5 elements
    // nothing is wrong with for() but you can do this too, for more complex cases.
    generate(g1.begin(), g1.end(), [](){ static int i = 1; return i++; });

    cout << "Output of begin and end\t:\t";
    // so called range-based for
    for (auto value : g1 )
        cout << value << '\t';

    cout << endl << endl;
    cout << "Output of rbegin and rend\t:\t";
    // type of ir is deduced. It's not same as C99's auto  keyword
    for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir)
        cout << '\t' << *ir;

    return 0;
}

In first for() loop the auto keyword is deduced to type of container's element. Use of this keyword saves from writing long nested typenames and also is very useful in template code, along with typedecl

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42