C++ 在哪些函数中我必须使用std:?
这是我的第一篇帖子,我是初学者。我在学习 我已经读到使用C++ 在哪些函数中我必须使用std:?,c++,std,C++,Std,这是我的第一篇帖子,我是初学者。我在学习 我已经读到使用名称空间std被认为是一种不好的做法,我有点理解为什么,所以每次我都尝试使用std::。 我知道我必须在cout和cin中使用它 我的问题是,我还需要在哪些函数中使用std:?我在哪里可以看到列表或类似的东西 谢谢。名单真的很大。但是如果您在需要时不使用它,编译器会警告您。如果您使用一个像样的IDE,它会有所帮助。有很多是免费使用的,所以只需挑选一个 几乎所有您可能调用的东西都需要某种名称空间前缀,其中std::是最常见的 请注意,如果您这
名称空间std
被认为是一种不好的做法,我有点理解为什么,所以每次我都尝试使用std::
。
我知道我必须在cout
和cin
中使用它
我的问题是,我还需要在哪些函数中使用std:?我在哪里可以看到列表或类似的东西
谢谢。名单真的很大。但是如果您在需要时不使用它,编译器会警告您。如果您使用一个像样的IDE,它会有所帮助。有很多是免费使用的,所以只需挑选一个 几乎所有您可能调用的东西都需要某种名称空间前缀,其中std::是最常见的 请注意,如果您这样做也没那么糟糕:
using std::cout;
using std::endl;
cout << "This is a string" << endl;
然后你基本上把所有的std都拉进去——这增加了发生名称冲突的可能性。在上面检查/测试这段代码
仅约定C++标准库可以有名称空间STD.< /P> C++标准中的所有东西都从<代码> STD<代码>开始。从C中采用的东西可能有点不同。例如,
std::pow
与pow
并不完全相同。跟踪到(据我所知)草案的最新版本。你必须做一点推敲,才能找到最符合你正在编译的发布标准的修订版。堆栈溢出上的某个地方有一个映射列表,但我现在找不到。找到了吸盘:cppreference.com是探索STL和获取最新在线文档的好地方。从一开始就使用std是好的。玩得高兴该标准禁止将您自己的函数添加到std
。如果这样做,则程序的行为是未定义的。
using namespace std;
#include <iostream>
namespace std{
static int Add(int a, int b)
{
return a + b;
}
}
namespace math{
static int Add(int a, int b)
{
return a + b;
}
}
static int Add(int a, int b)
{
return a + b;
}
using namespace std;
using namespace math;
int main() {
std::cout << std::Add(1,2) << std::endl; //OK
std::cout << math::Add(1,2) << std::endl; //OK
std::cout << ::Add(1,2) << std::endl; //OK
std::cout << Add(1,2) << std::endl;//ERROR. Ambiguous call to "std::Add" or "math::Add" or local function "Add" // Comment this line for fix the error or complete the correct namespace or delete using namespace statements.
}
main.cpp:29:16: error: call to 'Add' is ambiguous
std::cout << Add(1,2) << std::endl;//ERROR. Ambiguous call to "std::Ad...
^~~
main.cpp:4:14: note: candidate function
static int Add(int a, int b)
^
main.cpp:11:14: note: candidate function
static int Add(int a, int b)
^
main.cpp:17:12: note: candidate function
static int Add(int a, int b)
^
1 error generated.
compiler exit status 1
std::Add
math::Add
::Add
std::cout
std::string