1

Since, i got to know that writing "using namespace std;" in the header should be avoided. And now i find it pretty annoying to write "std::" before vector,string,cout,cin etc every time in the code. So, instead of that can i just write "using std::vector","using std::cout" etc in the header so that i don't have to write it again and again?? And,am I allowed to write "using namespace std;" in the header in a coding interview?

Can i use the way shown below??

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

using std::endl;
using std::vector;
using std::string;
using std::cout;
using std::pair;
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/236280/discussion-on-question-by-lame-things-is-it-ok-to-write-using-stdvector-in). – Bhargav Rao Aug 22 '21 at 06:53

2 Answers2

0

Is it ok to write “using std::vector;” in the header?

It's not ideal. Basically, most of the same reasons to not use using namespace std; apply to not use using std::vector. The problems are just slightly fewer given that the list of "used" identifiers is smaller.

Consider a case where the user of your header also declares ::vector or includes another header that declares it.

Also consider the reader of your code. How are they supposed to know that vector that you use is the standard one and not some other template? Sure, they can find it out if they go reading through all the headers, but why make it harder to the reader?


Basically, you shouldn't add anything in the global namespace unless you cannot avoid it. Using your own namespace would reduce the problem with name conflicts with other people's code:

namespace lame_things {
    using std::endl;
    using std::vector;
    using std::string;
    using std::cout;
    using std::pair;
}

The second problem still applies though.


And,am I allowed to write "using namespace std;" in the header in a coding interview?

That depends on the interviewer.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Ok,so the only answer is that i have to use std::, every time – Lame Things Aug 21 '21 at 18:07
  • @LameThings You don't "have to". It's just (subjectively) better to do so. – eerorika Aug 21 '21 at 18:07
  • I don't really get the argument of reading code. It may be different `vector`, but it doesn't (shouldn't) matter (just like `auto` or `concept`). – apple apple Aug 21 '21 at 18:08
  • @appleapple It can matter. If I have a `std::vector`, I want to know whether I can call your function or whether I have to convert first. – eerorika Aug 21 '21 at 18:10
  • @eerorika fair point, I somewhat agree not to eliminate namespace from declaration. But say, consider `using int_vec = std::vector;` from another answer, you still need to lookup what `int_vec` is. (or any type alias like `value_type`, `pointer_type`). It's not really a problem by `using std::vector`. – apple apple Aug 21 '21 at 18:34
  • @appleapple `But say, consider ... from another answer, you still need to lookup what int_vec is`. I agree. Thus, I don't see it as particularly useful thing to do in the namespace scope either. It could be reasonable for a very cumbrsome template instantiation, but `std::vector` is (subjectively) not cumbersome. `or any type alias like value_type, pointer_type` those are typically used for template argument dependant types, which are a good use case for type aliases and not what the question seems to be about in my interpretation. – eerorika Aug 21 '21 at 18:41
  • @eerorika well, just saying generally reader would probably not having problem parse what that unqualified `vector` is. (not saying `using std::vector` in top level namepace in header file is a good idea) – apple apple Aug 21 '21 at 18:47
0

you can use typedef like this

typedef std::vector<int> int_vec;

and you can use this later in the code like this

int_vec v;

or if you don't care about readablity which you should you can do this

typedef std::vector<int> v;

you might want to look at this too https://learn.microsoft.com/en-us/cpp/cpp/aliases-and-typedefs-cpp

OR

you can also use using as ninja said

using  int_vec = std::vector<int>;

refer to the link I posted above for the docs

Ibrahim
  • 798
  • 6
  • 26