先说结论,stdio.h当中有一个remove()用以删除文件,alorigthm当中有一个remove()用来将元素移动到末尾并且返回。
顺手include的iostream当中包含了cstdio,其中就有stdio.h。这样导致代码补全能够补出remove(),但是并不是我想要的remove()。
alorigthm当中的std::remove()才是我想要的那个功能,但是注意std命名空间。没有及时发现这个问题是因为:
using namespace std;
故而谨慎使用命名空间的性质,慎重使用using namespace std
这是我在使用VsCode写CPP的时候遇到的一个问题。
情况是这个样子的,在写leetcode的时候需要一个删除vector当中的元素的函数,这个时候我选择使用了erase()。
就得到了下面的代码:
vector<int> vec(5,1);
vec[2]= 2;
vec.erase(remove(vec.begin(),vec.end(),2),vec.end());
看起来并没有什么问题。
但是编译不通过,remove()出现了报错。一看感觉不对啊,一看函数实现int remove()?在stdio.h当中?
int remove(const char *_Filename)
回看整个文件,原来是少了alorigthm,引入alogrithm后程序成功运行起来了,可喜可贺。
但是为什么呢,有这么一个代码提示:
inline std::vector<int>::iterator std::remove<std::vector<int>::iterator, int>(std::vector<int>::iterator, std::vector<int>::iterator, const int &)
问题出现了!就是std命名空间,我这有个开始学cpp时候的习惯,using namespace std。这个习惯让我只要引入alorigthm就不需要在意命名空间的问题,就造成了这次出现这个问题。
还是要慎重使用全局的命名空间的使用,还是得多打两个字std::remove()。