-2

I am really confused about how we use std libraries in the header. When to use which library. Is "using namespace std" and conio.h different..?? Or are they the same thing. And what is the difference between "iostream" and "iostream.h". These things are making me really confused.....

RE8106
  • 1
  • 1
  • 2

2 Answers2

1
  1. std is the namespace for the C++ standard library - for example, string actually belongs to this namespace, so the full name for string is std::string. using namespace std tells the compiler that we want to access the resources in this namespace - giving us global, or direct access to the string it holds. Take a look at Pete's comment for more details.

  2. The C++ standard library contains many different packages that can be included by their headers, one of which is <iostream>, more std headers can be found here: http://www.cplusplus.com/reference/

  3. conio.h looks like an older DOS-specific C header that is not popular anymore.

  4. iostream.h was renamed to iostream as a standard at some point: http://members.gamedev.net/sicrane/articles/iostream.html

Also see: Why is "using namespace std" considered bad practice?

Edits thanks to Pete!

Community
  • 1
  • 1
J. Song
  • 85
  • 6
  • 1
    `using namespace std;` doesn't "tell the compiler which `string` we're using"; it tells the compiler to, essentially, treat **all** names that are defined in `std` as if they were also defined in the global namespace. You could define your own name `string` in the global namespace, and `using namespace std;` would then guarantee that whenever you referred to `string` it would be ambiguous. That aside, good answer. – Pete Becker Oct 19 '16 at 12:52
  • Incidentally, `conio.h` comes from DOS, not Windows. – Pete Becker Oct 19 '16 at 12:52
  • Good distinctions to point out, thanks! will edit – J. Song Oct 19 '16 at 13:31
0

It just simply allows you to use the namespace std, which is the namespace of most standard C++ header libraries. If you use it, you won't have to put the std:: prefix on accessing, for example, std::cout or std::cin, which would now be just cout and cin respectively.

For example:

// without using namespace std
#include <iostream>

int main() {
    cout << "Hello World"; // error
    std::cout << "Hello World"; // outputs Hello World
    return 0;
}

Now with using namespace std:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World"; // outputs Hello World
    return 0;
}

conio.h is a C++ library that contains functions such as getch() and putch(). iostream.h is a pre-standard C++ library that was used before namespaces were introduced. iostream is a standard library that contains objects such as cin and cout.

Rax Weber
  • 3,730
  • 19
  • 30