简体   繁体   English

如何使用 std::sort 对静态初始化的二维数组进行排序?

[英]How to sort a statically initiallized 2-D array with std::sort?

I initialized a 2-D array ( n rows * 2 cols ), and I want to sort it by std::sort like this:我初始化了一个二维数组( n rows * 2 cols ),我想按std::sort对它进行排序,如下所示:

// before std::sort
4 6
3 3
2 3

// after std::sort
2 3
3 3
4 6

The cmp function is like this: cmp函数是这样的:

static bool cmp(int m1[2], int m2[2]) 
{
    if (m1[1] != m2[1])
        return m1[1] < m2[1];
    return m1[0] < m2[0];
}

All codes are here:所有代码都在这里:

#include <cstdio>
#include <algorithm>

using namespace std;

int ministers[1010][2];
int n, a, b;

static bool cmp(int m1[2], int m2[2]) 
{
    if (m1[1] != m2[1])
        return m1[1] < m2[1];
    return m1[0] < m2[0];
}

int main()
{
    scanf("%d%d%d", &n, &a, &b);
    for (int i = 0; i < n; i++)
        scanf("%d %d", &ministers[i][0], &ministers[i][1]);
    std::sort(ministers, ministers+n, cmp);
    return 0;
}

But the CLione editor gives me a static error: static error但是 CLione 编辑器给了我一个静态错误:静态错误

And when I tried to compile this code, compiling error occurred:当我试图编译这段代码时,发生了编译错误:

====================[ Build | Vijos | Debug ]===================================
"F:\Program Files\JetBrains\CLion 2021.2.3\bin\cmake\win\bin\cmake.exe" --build E:\MyFilesCosec\CLionProjects\Vijos\cmake-build-debug --target Vijos -j 16
[1/2] Building CXX object CMakeFiles/Vijos.dir/main.cpp.obj
FAILED: CMakeFiles/Vijos.dir/main.cpp.obj 
"F:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\c++.exe"   -g -MD -MT CMakeFiles/Vijos.dir/main.cpp.obj -MF CMakeFiles\Vijos.dir\main.cpp.obj.d -o CMakeFiles/Vijos.dir/main.cpp.obj -c E:/MyFilesCosec/CLionProjects/Vijos/main.cpp
In file included from F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:62,
                 from E:/MyFilesCosec/CLionProjects/Vijos/main.cpp:2:
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h: In instantiation of 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]':
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1885:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]'
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1971:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]'
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:4866:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = int (*)[2]; _Compare = bool (*)(const int*, const int*)]'
E:/MyFilesCosec/CLionProjects/Vijos/main.cpp:19:42:   required from here
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1850:3: error: array must be initialized with a brace-enclosed initializer
   __val = _GLIBCXX_MOVE(*__i);
   ^~~~~
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1852:17: error: invalid array assignment
        *__first = _GLIBCXX_MOVE(__val);
                 ^
In file included from F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:61,
                 from F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:62,
                 from E:/MyFilesCosec/CLionProjects/Vijos/main.cpp:2:
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_heap.h: In instantiation of 'void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]':
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1672:23:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]'
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1933:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]'
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1948:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = int (*)[2]; _Size = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]'
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1968:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]'
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:4866:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = int (*)[2]; _Compare = bool (*)(const int*, const int*)]'
E:/MyFilesCosec/CLionProjects/Vijos/main.cpp:19:42:   required from here
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_heap.h:341:15: error: array must be initialized with a brace-enclosed initializer
    _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
               ^~~~~~~
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_heap.h: In instantiation of 'void std::__pop_heap(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]':
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1675:19:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]'
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1933:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]'
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1948:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = int (*)[2]; _Size = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]'
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:1968:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int*, const int*)>]'
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:4866:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = int (*)[2]; _Compare = bool (*)(const int*, const int*)]'
E:/MyFilesCosec/CLionProjects/Vijos/main.cpp:19:42:   required from here
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_heap.h:251:18: error: array must be initialized with a brace-enclosed initializer
       _ValueType __value = _GLIBCXX_MOVE(*__result);
                  ^~~~~~~
F:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_heap.h:252:17: error: invalid array assignment
       *__result = _GLIBCXX_MOVE(*__first);
                 ^
ninja: build stopped: subcommand failed.

I initialized a 2-D array ( n rows * 2 cols ), and I want to sort it by std::sort [...]我初始化了一个二维数组( n rows * 2 cols ),我想按std::sort [...]

If the col count is always 2, then you should have used a如果 col 计数始终为 2,那么您应该使用

as a suitable data structure here.作为一个合适的数据结构。 This will help to compare the elements by simply the comparison operator of std::pair .这将有助于通过std::pair的比较运算符来比较元素。

Example using the array of std::pair<int, int> you might do使用std::pair<int, int>数组的示例,您可能会这样做

std::pair<int, int> ministers[1010]; // array of  pair of ints
int n;  std::cin >> n;

for (int i = 0; i < n; i++)
    std::cin >> ministers[i].first >> ministers[i].second;
// compare function is a lambda
std::sort(ministers, ministers + n
       , [](const auto& m1, const auto& m2) { return m1 < m2; });
   // use of std::pair<int, int>::operator<  --> ^^^^^^^^^^^^^^^

Live demo现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM