我有一个正在使用std :: vector的windows上写的程序。它可以正常工作,但是当我在Linux上编译时,我会收到一个错误,上面写着:
'排序'在此范围中未声明
我需要使用一些linux友好版本吗?
class Bigun {
private:
std::vector<Other*> others;
};
void Bigun::sortThoseOthers() {
sort(others.begin(), others.end(), compareOthers);
}
std::vector
中的任何一个平台上都没有函数 sort
,所以我认为您使用的是 std::vector
的 std::sort
。
这很好。
错误消息建议两件事:
-
您正在编写
sort
,而不是std::sort
。只要您编写using namespace std
,这将起作用,尽管使用完全合格的名称更好。继续前进。 -
您没有编写
。#include <algorithm>
,而是依靠" transive include"&mdash;也就是说,假设其他标题本身包括<algorithm>
,这很可能是偶然的在Visual Studio实现的情况下,而不是Libstdc 或Libc 。您应始终包含适当的标准标头,以保证可移植性。请勿在某些特定系统上使用它们的程序似乎在没有它们的情况下工作。
在这里这样做,我敢打赌你的问题会消失。
通常,除非标准合规性和/或工具链错误,标准功能在操作系统之间是相同的。这就是为什么它们是标准的。
#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