我必须在哪些函数中使用 std::?
In what functions do I got to use std::?
这是我的第一个post,我是初学者。我在学习。
我读到过使用 using namespace std
被认为是一种不好的做法,我有点理解为什么,所以我每次都尝试使用 std::
。
我知道我必须在 cout
和 cin
.
中使用它
我的问题是,我还必须在哪些其他函数中使用 std::?我在哪里可以看到列表或类似的东西?
谢谢。
名单真的很大。但是如果你在需要的时候不使用,编译器会警告你。如果你使用像样的 IDE,它会有所帮助。有一堆可以免费使用,挑一个就好了。
几乎所有你可能调用的东西都需要某种命名空间前缀,std:: 是最常见的。
请注意,如果您这样做,情况还不错:
using std::cout;
using std::endl;
cout << "This is a string" << endl;
但如果你这样做:
using namespace std;
那么您基本上是在引入所有 std -- 这增加了发生名称冲突的可能性。
Check/test 此代码在 https://repl.it/@JomaCorpFX/NameCollision#main.cpp
#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
如果您键入 "using namespace std" 语句,您可能会在函数 name/signature 中发生冲突,另一个 header 可能包含没有命名空间的相同函数签名。
如果您包含一个新的 header,您的原始代码可能会中断,该新 header 可以包含 1 或 n 个函数,这些函数将签名与您的代码进行数学运算。编译器在代码中显示错误,很多令人头疼的问题。
最佳做法是使用完整名称,以避免出现这种情况。花更多的时间,获得更多的可读性和更少的错误。
std::Add
math::Add
::Add
std::cout
std::string
按照约定,只有 C++ 标准库可以具有命名空间 std。
这是我的第一个post,我是初学者。我在学习。
我读到过使用 using namespace std
被认为是一种不好的做法,我有点理解为什么,所以我每次都尝试使用 std::
。
我知道我必须在 cout
和 cin
.
我的问题是,我还必须在哪些其他函数中使用 std::?我在哪里可以看到列表或类似的东西?
谢谢。
名单真的很大。但是如果你在需要的时候不使用,编译器会警告你。如果你使用像样的 IDE,它会有所帮助。有一堆可以免费使用,挑一个就好了。
几乎所有你可能调用的东西都需要某种命名空间前缀,std:: 是最常见的。
请注意,如果您这样做,情况还不错:
using std::cout;
using std::endl;
cout << "This is a string" << endl;
但如果你这样做:
using namespace std;
那么您基本上是在引入所有 std -- 这增加了发生名称冲突的可能性。
Check/test 此代码在 https://repl.it/@JomaCorpFX/NameCollision#main.cpp
#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
如果您键入 "using namespace std" 语句,您可能会在函数 name/signature 中发生冲突,另一个 header 可能包含没有命名空间的相同函数签名。
如果您包含一个新的 header,您的原始代码可能会中断,该新 header 可以包含 1 或 n 个函数,这些函数将签名与您的代码进行数学运算。编译器在代码中显示错误,很多令人头疼的问题。
最佳做法是使用完整名称,以避免出现这种情况。花更多的时间,获得更多的可读性和更少的错误。
std::Add
math::Add
::Add
std::cout
std::string
按照约定,只有 C++ 标准库可以具有命名空间 std。