declared or defined???

It's been quite a while I'm struggling to resolve the exact usage of "using namespace std". I doubt if all classes are declared in that special namespace 'std',but not defined,right?? Because when we want to use,let's say vector or stack,we have to include its' respective header file,I guess that's where its' meaning has been defined,aka defined what it means to be stack or vector. Kindly help me to resolve my doubt and I'd be thankful to anyone who will help me
The only reason you use "using namespace std;" is so that you don't have to write std:: in front of all the standard names (e.g. std::vector, std::cout, std::size_t, and so on). The header files contain declarations (and sometimes definitions too) so in order to actually use the standard library you have to include the headers that you need.
Last edited on
Namespaces are there to prevent large projects from having problems with duplicate names.

namespace a
{
int x;
}

namespace b
{
int x;
}

x = 3; //which x? compiler error.
a::x = 3;//ok

using namespace b;
x = 5; // ok, b::x is used
a::x = 3; //ok, over-rides the using command above

std is just the namespace with the standard c++ language objects inside.
using applied to it bypasses the std:: but is currently considered bad form (there are many posts on why not to do it in the forums).
Last edited on
Thanks @Peter87 ! So,header files are included as they contain declaration and sometimes definition too as you have said- that's comprehensible,but what's doubtful is that I don't get it's where the definition is there ? std or header file?? As per your explanation,I suppose definition is in 'std' namespace,right?? Kindly let me know further
Thanks @jonnin. I do understand that namespace are there to avoid any name conflicts,but what's making me confused is it's where the definition is there??std or header file??
both.
the definitions of functions used in the c++ language are inside a header file. Inside that header file, they are ALSO inside the namespace std.

so for example

<iostream.h>
has something like (overly simplified)

namespace std
{
void cout(string s); //or whatever it really looks like...
... bunch of stuff
}
closed account (48T7M4Gy)
'std' is the name of the namespace which incorporates all the various elements of the C++ Standard Library eg <vector>'s are part of the Standard Library and are in the std namespace hence std::vector etc

https://en.wikipedia.org/wiki/C%2B%2B_Standard_Library
I don't get it's where the definition is there ? std or header file??

Another way to see this, is that they are defined/declared in the header files, but their actual names are "std::cout" and "std::vector", not "cout" or "vector". <iostream> provides std::cout. <vector> provides std::vector. You can strip that prefix for your convenience, although many people choose not to.
Hello SoumyadeepRoy,

Not sure if I a right here, but form what I have been able to put together is a header file like "string" has most if not all of its proto types of declarations wrapped in the "std" namespace and the definitions for all these functions are likely found in a ".dll" or ".lib" file because I have yet to find any ".cpp" files with the function definitions for what is in header files.

Hope that helps,

Andy
Topic archived. No new replies allowed.