-4

I would like to use the statements without std:: in my c++ code on Linux operating system (e.g. cout instead of std::cout, map<> instead of std::map<>, etc.). What headers are necessary to perform it?

Roloka
  • 201
  • 2
  • 9
  • 3
    `using namespace std;` anyway, http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Danh Jan 13 '17 at 08:09
  • 1
    No, not through header. No, it's not OS-dependent. –  Jan 13 '17 at 08:09
  • 2
    You would use the same headers, but write `using namespace std;` at the top of your code. [**However, this is a bad idea**](http://stackoverflow.com/q/1452721/1171191). Don't do it. – BoBTFish Jan 13 '17 at 08:09
  • 2
    Pretty much any C++ book will cover this in chapter one. You do have [a decent C++ book](http://stackoverflow.com/q/388242/253056), don't you ? – Paul R Jan 13 '17 at 08:23
  • 3
    Why on earth the close votes? It's a terrible idea, but it's not asking for a recommendation of a tool or library, and it's not a typographical error. – Martin Bonner supports Monica Jan 13 '17 at 08:36
  • 1
    I once used to think like you and wanted to get rid of the `std::` everywhere. But I just gave up, because you cannot use `using namespace std` cleanly at global or namespace scope in a header file anyway, and the resulting inconsistencies between `*.h` files and `*.cpp` files turned out to be a cure worse than the disease. So just forget about it. Use `std::` everywhere. Believe me, you will soon start to *like* it anyway. – Christian Hackl Jan 13 '17 at 10:24

3 Answers3

7

As commenters have pointed out: there's no include to do this, you just need to include your files as you normally would, and then write a using namespace statement

#include <iostream> // cout
#include <map> // map

using namespace std; // bring this entire namespace into scope

However; you should note, and commenters have pointed out, this is a terrible idea especially in a header file. For the extra few characters you need to write, you could save yourself hours of trouble later. If you're truly against writing std::, consider limiting the scope that you do this in

{
    // lots of console printing:
    using std::cout;
    cout << "";
    ...
}
// Now you'd need to write std::cout again

This is OS-independent (the solution for Linux is the same for other operating systems)

Community
  • 1
  • 1
Tas
  • 7,023
  • 3
  • 36
  • 51
  • To be precise, it's a terrible idea at global scope or namespace scope in a header file. It can be acceptable (IMO) inside of a definition of an inline function in a header file. – Christian Hackl Jan 13 '17 at 10:22
2

If you have classes you want to bring into the default namespace you can alias them with using. For example:

using std::cout;

You can import an entire namespace with using namespace, but you'll want to read and understand "Why is using namespace std considered bad practice?" first.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
1

It's not really a matter of #including some header, you can omit the explicit namespace by using the using namespace statement.
If you want to omit (really: bring all the std:: namespace items into the global namespace, which may not be really good practice), you can use:

using namespace std;

usually after the #includes, but before any code referencing std stuff.

roalz
  • 2,699
  • 3
  • 25
  • 42