-6

I've noticed that some people prefer

#include <vector>

and others prefer

using std::vector;

Is there a difference? I couldn't find a post on this on SO.

Changing from one to another in my code procures no less/additional errors.

Thank you

2 Answers2

1

#include <vector> is a must when you use vector.

using std::vector; is optional as this saves you from typing std:: when declaring a vector for example.

vector<int> v;         // no need  prefix std::
artm
  • 17,291
  • 6
  • 38
  • 54
1

These are related to some degree, but neither replaces the other.

#include <vector>

This makes a declaration of vector visible, so you can define objects of type vector<T> (for some type T), define functions that take a vector by value, return a vector by value, etc.

using std::vector;

This makes the declaration of vector visible without your having to qualify it with the name of the namespace in which it resides (std) so, for example, to define a vector of ints, you can use:

vector<int> x;

...instead of:

std::vector<int> x;

...as you need to when you've just done the #include <vector> without the using declaration.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111