C++ Linux上的std::vector sort()?
我有一个在Windows上编写的程序,在那里我使用std::vector的排序函数。它工作得很好,但是当我在Linux上编译时,我得到一个错误,上面写着: 未在此作用域中声明“sort” 我需要使用Linux友好的版本吗C++ Linux上的std::vector sort()?,c++,linux,sorting,vector,C++,Linux,Sorting,Vector,我有一个在Windows上编写的程序,在那里我使用std::vector的排序函数。它工作得很好,但是当我在Linux上编译时,我得到一个错误,上面写着: 未在此作用域中声明“sort” 我需要使用Linux友好的版本吗 class Bigun { private: std::vector<Other*> others; }; void Bigun::sortThoseOthers() { sort(others.begin(), others.end(), com
class Bigun {
private:
std::vector<Other*> others;
};
void Bigun::sortThoseOthers() {
sort(others.begin(), others.end(), compareOthers);
}
类比根{
私人:
性病:其他;
};
void Bigun::sortThoseOthers(){
排序(others.begin()、others.end()、compareOthers);
}
在std::vector
中没有函数sort
,因此我假定您使用的是std::sort
的迭代器范围为std::vector
这是正确的
错误消息提示了两件事:
- 您正在编写
,而不是sort
。只要您使用namespace std编写std::sort
,这就行了,不过使用完全限定名更好。继续
- 您没有编写
#include)是否包含所需的标题?请添加您的代码。没有“std::vector的排序函数”。有
,但它与std::sort
没有直接关联;它是一个独立的函数,可用于任意随机访问迭代器。无论哪种方式,如果您的代码不起作用,请向我们提供问题的详细信息;很抱歉,我想我不需要这样一个问题的例子,但我明白为什么我会这样做。下次我会这么做的,我很感谢各位的提醒。现在编辑这个问题还不晚。问题应该是未来访问者的知识宝库!说得好!好了!我希望有足够的代码来表示我遇到的问题。你是对的-我没有包括,当我包括的时候,它起了作用。非常感谢。幸运的猜测!std::vector
#include <vector> #include <algorithm> #include <iostream> int main() { std::vector<int> v{5,3,4,1,2}; std::sort(v.begin(), v.end()); for (const auto& el : v) std::cout << el << ' '; std::cout << '\n'; } // Output: 1 2 3 4 5