Re: what does using namespace std mean?

P

Peter van Merkerk

soni29 said:
hi,
i did some c++ back in college, currently work mainly with java, and
i'm trying to refresh myself in c++, after starting a tutorial online
i noticed that the author used the line using namespace std. what is
that? i've never seen that in college or in my c++ text. i see the
site has the #include<iostream.h> to bring in cin and cout but not
sure why the std line is there. the code the site had is:

#include <iostream.h>
#include <string>

using namespace std;
int main() {
string name;
cout << "What is your name?";
cin >> name;
cout << "Welcome, " << name << ".";
return 0;
}

Namespaces are used to avoid name clashes with other libraries. C++
namespace are _somewhat_ similar to Java packages. Standard library
class and functions reside in the std namespace. Without the 'using
namespace' directive you would have to explicitly add std:: before every
identier of the standard library:

int main() {
std::string name;
std::cout << "What is your name?";
std::cin >> name;
std::cout << "Welcome, " << name << ".";
return 0;
}

The 'using namespace std;' directive pulls the identifiers in the std
namespace into the current namespace so that you don't have to
explicitly specify that you are refering to a identifier from the std
namespace. The 'using namespace' directive should be used judiciously in
the smallest posible scope and never in header files. To liberal use of
'using namespace' directives may lead to unexpected results.

BTW. <iostream.h> is non-standard header it should be <iostream>. If the
tutorial can't get these simple things right one might wonder about the
quality and accuracy of rest of the tutorial
 
S

soni29

hi,
thank you for the response, you have posted this code:

int main() {
std::string name;
std::cout << "What is your name?";
std::cin >> name;
std::cout << "Welcome, " << name << ".";
return 0;
}

if i use this method of std:: then there should be no need for the
#include<iostream> correct, as long as all i'm doing is cin and cout?
are std library and the <iostream> same? sorry i don't have the api
on me, i'm currently using a unix c++ complier, from college, and i
don't see and api around on it that the college is providing us. i'll
try searching the web, but thought i'd case incase i can't find my
answer.

Thank you again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top