std::vector sort() on Linux?
std::vector sort() on Linux?
我有一个正在使用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
(LINE DEMO,Linux上)
相关文章:
- VS2019 - Sudo Remote Debugging on Linux with Cmake project
- SFML 交叉编译 for Windows on Linux.
- OpenAL C++ on Linux
- Valgrind on codeblocks (linux)
- CMake FindJNI issue on linux
- pthread_self on Linux
- std::vector sort() on Linux?
- Building OpenCV 2.4.11 on Linux with `libopencv_ffmpeg.so`
- 在Linux上使用QT,有没有办法禁用"Always on Top"?
- 在 Windows for Linux on Intel 上编译C++库
- 计算"copying a binary file in c++ on linux"消耗时间的最佳方法是什么?
- C++ [[gnu::visibility( "default" )]] vs __declspec(dllexport) on Windows 和 Linux
- XRE_InitEmbedding2 SIGSEGV on Linux with XULRunner 15
- boost::mutex::timed_lock not on Linux
- Netbeans C/C++ on Linux "step in to" Linux C 运行时代码?
- gdb fork() exec on Linux
- flock-ing a C++ ifstream on Linux (GCC 4.6)
- C++ GetModuleBase on linux
- _vscwprintf on Mac OS X/Linux
- RTLD_LOCAL and dynamic_cast on Linux