Difference between std :: cout and cout?

4

EmCc ++ because some use std::cout and others use only cout is only what the programmer thinks is best?

    
asked by anonymous 07.09.2016 / 22:54

2 answers

5

In C ++ all names defined in global scope belong to a namespace .

A name is in global scope when it is declared outside a function and a class. For example

#include <iostream>

int a = 1;

int main()
{
    int b = 2;
    std::cout << "a=" << a << " b=" << b << "\n";
    {
       int c = 3;
       std::cout << "a=" << a << " b=" << b << " c=" << c << "\n";
    }
}

In the example above, the a variable is in the global scope (we say it is a "global variable"), and the b and c fault variables are in a local scope ( we say that they are "local variables"). The local variable b is in the scope of the main function, and the local variable c is in the scope of the block opened by the { key, and closed by the } key that comes next.

Well, all of this was to say that all names that are in global scope belong to a namespace . A namespace, as the name says, is a "namespace," or a "set of names."

When a global scope name does not belong explicitly to a namespace, then the name automatically belongs to the global namespace . Therefore, in the above example, the global variable a belongs to the global namespace.

To indicate that a global scope name belongs to a namespace, we need to give the name of the namespace it belongs to.

#include <iostream>

int a = 1;

namespace x
{
   int a = 2;
};

int main()
{
    int b = 3;
    std::cout << "a=" << a << " b=" << b << "\n";
    {
       int c = 3;
       std::cout << "a=" << a << " b=" << b << " c=" << c << "\n";
    }

    std::cout << "a=" << a << " x::a=" << x::a << "\n";
}

In the above example we have two global variable a : one that belongs to the global namespace (initialized with 1) and one that belongs to the namespace x (initialized with 2).

Note that to refer to the a variable that belongs to the x namespace we need to do this explicitly, using the x::a notation.

There are two ways to simplify encoding and not repeat the name of the namespace. Both ways use the directive using .

The first way simplifies reference to just one namespace name. In the example above, shortly after declaring namespace x we would use the using x::a; directive. In this way, we could reference the variable x::a just by writing a . However in the above example this will not work, as it will conflict with the variable a (initialized with 1) belonging to the global namespace!

In the following example the variable x::y is referenced in this simplified way.

#include <iostream>

int a = 1;

namespace x
{
   int a = 2;
   int y = 2;
};
using x::y;

int main()
{
    int b = 3;
    std::cout << "a=" << a << " b=" << b << "\n";
    {
       int c = 3;
       std::cout << "a=" << a << " b=" << b << " c=" << c << "\n";
    }

    std::cout << "a=" << a << " x::a=" << x::a << "\n";
    std::cout << "x::y=" << y << "\n";
}

The other way to simplify access to variables belonging to a namespace is by using the "using namespace" directive. In this way all names belonging to the namespace can be referenced without explicitly naming the namespace.

The example below illustrates the use of using namespace (note the namespace z ).

   #include <iostream>

   int a = 1;

    namespace x
    {
       int a = 2;
       int y = 2;
    };
    using x::y;

    namespace z
    {
      int aa = 1;
      int bb = 2;
    };
    using namespace z;

    int main()
    {
        int b = 3;
        std::cout << "a=" << a << " b=" << b << "\n";
        {
           int c = 3;
           std::cout << "a=" << a << " b=" << b << " c=" << c << "\n";
        }

        std::cout << "a=" << a << " x::a=" << x::a << "\n";
        std::cout << "x::y=" << y << "\n";
        std::cout << "z::aa=" << aa << " z::bb=" << bb << "\n";
    }

Okay, and what does all this have to do with std::cout ? The answer is: all global classes and objects of the standard C ++ language library are declared within the std namespace.

So, to reference a class or a standard C ++ library object, we need to specify that they belong to the std namespace.

Then we can use one of the 3 ways described above: explicitly specify the namespace (as in std::cout ), or use a dretive for each name in std ( using std::cout ), or use a single directive for all the names in std ( using namespace std ).

The third way, using namespace std , is not advised, because it frees access to all namespace names std , giving rise to potential conflicts.

However, it is acceptable to use using namespace std in small programs, sample programs, test programs, etc. It is all a matter of "common sense".

In addition, we can explicitly refer to a variable in the global namespace using the :: notation. For example, ::a is a reference to the name a declared in the global namespace.

(There was still talk about anonymous namespace, but this is for the next one.)

    
08.09.2016 / 01:42
9

std::cout is the full name of the object, including its "family name", the last name. A lot of people like to use it that way, eliminating any ambiguity.

Others prefer to use only the object name to get shorter. It's a more informal way of using it. To be possible, you must first indicate which family you will use. The main one of C ++ is just std . Then doing:

using namespace std;

dispenses the use of the last name in each object of the code that belongs to the std family.

See more in Why is it not good practice to use "std" namespace in C ++? to understand the advantages and disadvantages of it. But in essence it's taste.

An example .

    
07.09.2016 / 23:00