谁知道std

zzunicholas 2006-09-30 09:26:47
请问有人知道std是怎么定义的吗?

namespace是说明在h中还是cpp中

有了namesspace还需不需要加入防止重复编译的宏?
...全文
272 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlbdan 2007-04-01
  • 打赏
  • 举报
回复
namespcae std不在.h中, .h的文件是为C用的,是没有标准化之前的C++里为了和C兼容而保留的.
楼主可以写上
#include <iostream> //这样的文件里面有std
using namespace std;
然后查看一下iostream里面
那里面有一个好像是叫_STD_BEGIN和_STD_END的宏
它们的定义分别是namespace std{ 和 };
正好是一个std名字空间
QQ_370566617 2007-04-01
  • 打赏
  • 举报
回复
All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.
h9999h 2007-04-01
  • 打赏
  • 举报
回复
好好看书吧。
missilery 2007-04-01
  • 打赏
  • 举报
回复
namespace是C++关键字
在java中与其对应的关键字是package
jixingzhong 2006-09-30
  • 打赏
  • 举报
回复
Namespace std

All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.
jixingzhong 2006-09-30
  • 打赏
  • 举报
回复
Namespaces Published by Juan Soulie
Last update on May 17, 2006 at 12:58am

Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.
The format of namespaces is:

namespace identifier
{
entities
}


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

namespace myNamespace
{
int a, b;
}



In this case, the variables a and b are normal variables declared within a namespace called myNamespace. In order to access these variables from outside the myNamespace namespace we have to use the scope operator ::. For example, to access the previous variables from outside [myNamespace=] we can write:

general::a
general::b



The functionality of namespaces is especially useful in the case that there is a possibility that a global object or function uses the same identifier as another one, causing redefinition errors. For example:

// namespaces
#include <iostream>
using namespace std;

namespace first
{
int var = 5;
}

namespace second
{
double var = 3.1416;
}

int main () {
cout << first::var << endl;
cout << second::var << endl;
return 0;
}
5
3.1416



In this case, there are two global variables with the same name: var. One is defined within the namespace first and the other one in second. No redefinition errors happen thanks to namespaces.


using
The keyword using is used to introduce a name from a namespace into the current declarative region. For example:
// using
#include <iostream>
using namespace std;

namespace first
{
int x = 5;
int y = 10;
}

namespace second
{
double x = 3.1416;
double y = 2.7183;
}

int main () {
using first::x;
using second::y;
cout << x << endl;
cout << y << endl;
cout << first::y << endl;
cout << second::x << endl;
return 0;
}
5
2.7183
10
2.7183



Notice how in this code, x (without any name qualifier) refers to first::x whereas y refers to second::y, exactly as our using declarations have specified. We still have access to first::y and second::x using their fully qualified names.

The keyword using can also be used as a directive to introduce an entire namespace:

// using
#include <iostream>
using namespace std;

namespace first
{
int x = 5;
int y = 10;
}

namespace second
{
double x = 3.1416;
double y = 2.7183;
}

int main () {
using namespace first;
cout << x << endl;
cout << y << endl;
cout << second::y << endl;
cout << second::x << endl;
return 0;
}
5
10
3.1416
2.7183



In this case, since we have declared that we were using namespace first, all direct uses of x and y without name qualifiers were referring to their declarations in namespace first.

using and using namespace have validity only in the same block in which they are stated or in the entire code if they are used directly in the global scope. For example, if we had the intention to first use the objects of one namespace and then those of another one, we could do something like:

// using namespace example
#include <iostream>
using namespace std;

namespace first
{
int x = 5;
}

namespace second
{
double x = 3.1416;
}

int main () {
{
using namespace first;
cout << x << endl;
}
{
using namespace second;
cout << x << endl;
}
return 0;
}
5
3.1416




Namespace alias
We can declare alternate names for existing namespaces according to the following format:

namespace new_name = current_name;
sinall 2006-09-30
  • 打赏
  • 举报
回复
namespace是说明在h中还是cpp中
——.h

有了namesspace还需不需要加入防止重复编译的宏?
——需要

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧