-4

why do we put scope resolution operator before iterator whereas we do not use scope resolution operator before scores?

std::vector<double> scores;  
std::vector<double>::iterator pv;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
Dipok Dipu
  • 321
  • 1
  • 3
  • 6
  • Please note that C and C++ are different languages - use only the relevant language tag (C++ in this case). – kaylum Apr 13 '17 at 01:30
  • 3
    Because one of them is within a scope that needs resolving. – GManNickG Apr 13 '17 at 01:31
  • 1
    Possible duplicate of [Why does C++ need the scope resolution operator?](http://stackoverflow.com/questions/9338217/why-does-c-need-the-scope-resolution-operator) –  Apr 13 '17 at 01:35

2 Answers2

2

std is a namespace.
std::vector is a class template in the std namespace, which makes std::vector<double> a class.
std::vector<T>::iterator is a nested type under std::vector<T>.

If you want to define an object of type std::vector<double>, you use:

std::vector<double> obj;

If you want to define an object of type std::vector<double>::iterator, you use:

std::vector<double>::iterator iter;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

You're focusing on the wrong part. The first statement defines an object named scores. The second statement defines an object named pv. Neither of those names has a :: in front of it.

vector is the name of a template defined in the namespace std, so it is referred to as std::vector. iterator is the name of a type that is defined inside std::vector<double>, so it is referred to as std::vector<double>::iterator.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • 1
    To add to this: i.e. `std::vector::iterator` is a specific iterator for the `std::vector` type. And for example `std::list` has a specific iterator at `std::list::iterator`, Etc. – JHBonarius Apr 13 '17 at 14:20
  • I didn't understand the accepted ans at first glance. This answer cleared my doubts. Then I read the accepted ans again. Now I can understand the accepted answer also. Simply put `vector::iterator pv` declares `pv` as an iterator type variable which is inside vector class template. – Abinash Dash Jul 17 '20 at 11:02