-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?

6
  • 4
    You should probably read a couple of good beginners books as that should answer your questions here. Commented Aug 3, 2017 at 5:35
  • The why is because whomever wrote this code wants to iterate over the vector. Or am I missing the question? Commented Aug 3, 2017 at 5: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. Commented Aug 3, 2017 at 5:36
  • Possible duplicate of How to navigate through a vector using iterators? (C++) Commented Aug 3, 2017 at 5:36
  • 3
    The title of this question comes from outer space. Please fix. Commented Aug 3, 2017 at 5:40

2 Answers 2

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;

}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.