I initialized a 2-D array (n rows * 2 cols
), and I want to sort it by std::sort
like this:
// before std::sort
4 6
3 3
2 3
// after std::sort
2 3
3 3
4 6
The cmp
function is like this:
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
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.
CodePudding user response:
I initialized a 2-D array (
n rows * 2 cols
), and I want to sort it bystd::sort
[...]
If the col count is always 2, then you should have used a
as a suitable data structure here.
This will help to compare the elements by simply the comparison operator of std::pair
.
Example using the array of std::pair<int, int>
you might do
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< --> ^^^^^^^^^^^^^^^