0

What does this line with comment mean? The details is in the comment below:

#include <iostream>
#include <deque>
  
using namespace std;
  
void showdq(deque <int> g)
{
    deque <int> :: iterator it; /*scope resolution operator means we're getting this obj from this scope, angle bracket is used for template, it seems to be an object, but what does the word iterator doing here? 
    Deque<int>g is also passed as value, this could be avoided if it was passed as reference right? I'll delete this once i got the answer, thank you!*/

    for (it = g.begin(); it != g.end(); ++it)
        cout << '\t' << *it;
    cout << '\n';
}
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
Renz Carillo
  • 349
  • 2
  • 11
  • 1
    It's declaring iterator object of deque type. You can search for c++ iterator on google. – MarkSouls Apr 21 '21 at 04:14
  • 2
    `iterator` is a type defined within the `deque` class. – jkb Apr 21 '21 at 04:15
  • 2
    cppreference is the place to read up on things like this. See for instance https://en.cppreference.com/w/cpp/iterator, and https://en.cppreference.com/w/cpp/container/deque. Spend some time on it. It will be worth your while. – Captain Giraffe Apr 21 '21 at 04:58

2 Answers2

3

This seems to be a homework question so I'll try and give you pointers instead of the answer you hand in

deque <int> :: iterator it; 
/*scope resolution operator means we're getting this obj from this scope, 
angle bracket is used for template, it seems to be an object, 
but what does the word iterator doing here? 

:: scope resolution operator means you are looking for a name defined in the scope name (the name that is before the ::). E.g. just ::iterator, or iterator would be the name found in a global scope.

So here you have a scope deque<int>::, there is however no name deque<int> in the global scope, that is why you see using namespace std on the fourth line. This is considered bad practice.

So lets read this as std::queue<int>::iterator.

So when looking for the name iterator you should consult the documentation preferably at https://en.cppreference.com/w/cpp/container/deque

You find there that you are looking at std::queue and its members (the names it contains). These members describe different things, values, functions types and so on. Find the member type iterator and see what it says.

Now you just need to read up on what an iterator is, it is a thing that behaves like one of these https://en.cppreference.com/w/cpp/iterator.

Notice the operators applied to it later in the code. You see it = ..., it != ..., ++it and *it, these have their meaning and function described in the linked documentation.

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
1

Maybe not a answer to your question but, a tip for the future. If you do not want to care about iterators you could always use a for each-loop.

for (auto value: g) {
    cout << '\t' << value;
}
Lasersköld
  • 2,028
  • 14
  • 20