Click to See Complete Forum and Search --> : Stl


sattenapalli
November 27th, 2004, 06:50 AM
I am studying "Standard Template Library"from Deitle & Deitle .In that Book to define a vector he used following syntax.

#include<iostream>
using std::cout;
using std::cin;

std::vector<int> vec;

why can not we declare like this?
#include<iostream.h>

vector<int>vec;

why we can not use "cout","cin" directly rather than declaring "using std::cout,using std::cin?

explain in detail?

Luchin_plusplus
November 27th, 2004, 07:16 AM
...
why can not we declare like this?
#include<iostream.h>

vector<int>vec;


For the most part, "Because that's not C++". This is C++:


#include<iostream> // <<-- This is a C++ header
using namespace std; // Brings in things that are in "std::"

vector<int> vec;

cout<< something;
cin>> something_else;




The STL is C++, and thus the interface it provides is encloses in the "std" namespace istead of the global namespace. The same goes for the current headers (without the .h extension) that C++ provides.
It is then encouraged the usage of the new headers (<iostream> instead of <iostream.h>), because the new headers are C++-specific and may contain code that is not compatible with the C-implementation, such as function name overloading, template parameters, interface with C++ specific objects, and more.

You can still use the old-style headers, but if you do, you can not add a new-style header to your program. Ie.: they are not compatible. And in extension it is not C++, it is C "reloaded".
It is reccommended you go with the new headers to take advantag of the solutions C++ provides.

The "using namespace std" declaration, allows you to bring all of he components of the std namespace so that you can call them by their name. In other words, it allows you to use "cin" instead of "std::cin".

sattenapalli
November 27th, 2004, 07:26 AM
I am a beginner.I do not know keyword "using".In your explanation I did not understand what is "std" and what is global namespace.Please be clear about it?

Gabriel Fleseriu
November 27th, 2004, 08:15 AM
You will probably need to consult your favorite textbook, as we cannot provide all the definitions here.

"std" is a namespace. The "using" directive is used in this context to bring the contents of a namespace, or parts of it, into the current scope.
using namespace std; // brings the entire namespace std into the current scope
using std::cout; // brings std::cout into the current scope

Note that most tutorials and textbooks bloat the namespace std into the global namespace in their examples for simplicity. The alternative is to use the scope resolution operator :: to resolve the namespace, as ist is done in your example with vector.

Let me give you an advice: learn the fundamentals of C++ before beginning with the standard library. And don't maek the mistake to confound C and C++. As a small correction to Luchin_plusplus' post: <iostream.h> is not a C header, it is a pre-standard C++ header.

Andreas Masur
November 27th, 2004, 02:54 PM
The following basically answers all of your questions above...

Namespaces allow you to group a set of global classes, objects and/or functions under one name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.

The form of a namespaces is:

namespace identifier
{
namespace-body
}

where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example:

namespace general
{
int a, b;
}

In this case, a and b are normal variables integrated within the general namespace. In order to access these variables from outside the namespace you have to use the scope operator ::.

For example, to access the previous variables you would have to put:

general::a
general::b

The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error. For example:

#include <iostream>

namespace first
{
int var = 5;
}

namespace second
{
double var = 3.1416;
}

int main()
{
std::cout << first::var << std::endl;
std::cout << second::var << std::endl;

return 0;
}

In this case two global variables with the var name exist, one defined within namespace first and another one in second. No redefinition errors thanks to namespaces.

The using directive followed by namespace serves to associate the present nesting level with a certain namespace so that the objects and functions of that namespace can be accesible directly as if they were defined in the global scope. Its utilization follows this prototype:

using namespace identifier;

Thus, for example:

#include <iostream>

namespace first
{
int var = 5;
}

namespace second
{
double var = 3.1416;
}

int main()
{
using namespace second;

std::cout << var << std::endl;
std::cout << (var*2) << std::endl;

return 0;
}

In this case you have been able to use var without having to precede it with any scope operator.

You have to consider that the sentence using namespace has validity only in the block in which it is declared (understanding as a block the group of instructions within key brackets {}) or in all the code if it is used in the global scope. Thus, for example, if you had intention to first use the objects of a namespace and then those of another one you could do something similar to:

#include <iostream>

namespace first
{
int var = 5;
}

namespace second
{
double var = 3.1416;
}

int main()
{
{
using namespace first;
std::cout << var << std::endl;
}

{
using namespace second;
std::cout << var << std::endl;
}

return 0;
}

One of the best examples about namespaces is the standard C++ library itself. According to ISO C++ standard, the definition all the classes, objects and functions of the standard C++ library are defined within namespace 'std'.

Almost all compilers, even those complying with ISO standard, allow the use of the traditional header files (like iostream.h, stdlib.h, etc). Nevertheless, the ISO standard has completely redesigned these libraries taking advantage of the template feature and following the rule to declare all the functions and variables under the namespace 'std'.

The standard has specified new names for these "header" files, basically using the same name for C++ specific files, but without the ending '.h'. For example, 'iostream.h' becomes 'iostream'.

If you use the ISO-C++ compliant include files you have to bear in mind that all the functions, classes and objects will be declared under the 'std' namespace. For example:

#include <iostream>

int main()
{
std::cout << "Hello world" << std::endl;

return 0;
}

Although it is more usual to use using namespace and save you to have to use the scope operator :: before all the references to standard objects:

#include <iostream>

using namespace std;

int main()
{
cout << "Hello world" << endl;

return 0;
}

If you take a look at the last thing I wrote you will see what I meant. If you put a global using directive than you can access every variable, function, class, template etc. from that namespace in this case 'std'. That means of course that all the variables, function etc. in this namespace become global objects too. Everywhere in your code you can access the content of this namespace without the scope operator ::. That's of course not the real purpose of a namespace. Like I mentioned namespaces are designed to seperate variables, functions etc. But as I said in case of the STL is is more usual to declare the whole namespace...of course if you only use e.g. the string template you don't need to map the whole namespace... there you only use the using directive as follows:

using std::string;

This maps only the string template into your applications' namespace instead the whole 'std' namespace...

The following shows you the four different methods to map a namespace...

// Using the full member name, including the namespace it belongs to:
std::cout << "Hello World";


// By taking advantage of Using-Declarations:
using std::cout; // This declares cout in the current
// scope as synonym for std::cout
cout << "Hello World";


// By taking advantage of Using-Directives:
using namespace std; // Which specifies that the current
// scope can refer to names in the
// 'std' namespace without using
// full qualifiers. This is mostly
// used when porting legacy code.
cout << "Hello World";


// Using aliases:
namespace X
{
namespace Y
{
class Z { ... };
}
}

X::Y::Z // The full qualifier for 'Z' is
// 'X::Y::Z'

namespace w = X::Y; // This declares 'w' as an alias for
// namespace 'X::Y'

w::Z // Access 'Z' using 'w::Z'