C ++ namespace use

zhaozj2021-02-08  256

The C language provides a global namespace NameSpace to avoid global naming conflicts. For an instance, please pay attention to the following two header files:

// one.hchar func (char); class string {...};

// Somelib.hclass string {...};

If defined in the above manner, the two headers cannot be included in the same program because the String class conflicts. The so-called namespace is a method encapsulated by the library name, which is like a road wall in each library. For example: // one.hnamespace one {char func (char); class string {...};

// Somelib.hnamespace Somelib {class string {...};

Even if you use the String class in the same program, they don't have conflicts because they become: one :: string () and some of Somelib :: string ()

This way, you can distinguish between different classes or functions by declaring namespaces. For example, the C standard library defines the namespace: STD, which contains the container vector, examples are as follows: #include "stdafx.h" #include #include #include using namespace std;

INT main (int Argc, char * argv []) {const Int arraysize = 7; int [[arraysize] = {0, 1, 2, 3, 4, 5};

File: / / Define Container Vector Vector IVECT (IA, IA Arraysize);

Vector :: item (Ivect.Begin (), Ivect.eGin (), Ivect.end (), 4); if (it1 == Ivect.end ()) cout << "4 not found" << ELSE COUT << "4 found" << * IT1 << endl;

Return 0;}

The output is: 4 Found 4.

转载请注明原文地址:https://www.9cbs.com/read-2635.html

New Post(0)