这个问题在这里已经有了答案:
Why is "using namespace std;" considered bad practice?
(40 个回答)
去年关闭。
我想调用一个函数 reverse(BidirectionalIterator first, BidirectionalIterator last)
来自 <algorithm>
我的函数中的头文件,其名称也是 reverse(int)
.
代码:
#include<iostream>
#include<algorithm>
using namespace std;
class Solution{
public:
int reverse(int x){
string num = to_string(x);
reverse(num.begin(), num.end());
}
};
我认为它会根据传递的参数自动调用适当的函数,就像函数重载一样。但是,它没有。我试过:
namespace algo{
#include<algorithm>
}
但它给出了很多错误。
最佳答案
啊,现在您正在体验 StackOverflow 上人们的原因 always yelling about not using using namespace std;
.问题是您将整个命名空间带入全局命名空间,这会导致这样的冲突。
但是,如果删除该行,现在所有导入的函数都保留在 std
中。命名空间,所以你可以这样做:
#include<iostream>
#include<algorithm>
// BAD
// using namespace std;
class Solution{
public:
int reverse(int x){
std::string num = std::to_string(x);
std::reverse(num.begin(), num.end());
return std::stoi(num); // Don't forget to return!
}
};
关于C++:如何从我自己的函数中的头文件中调用一个函数(比如 funA()),它的名字也是 funA()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64537800/