排序2D C++子阵列中的第一个元素数组

发布于 2025-02-12 12:14:21 字数 394 浏览 5 评论 0原文

我正在尝试按第一个元素对C ++子阵列进行排序。 我的代码当前是这样设置的:

int umbrellas[3][2] = {{5, 6}, {2, 7}, {9, 20}};

int n = sizeof(umbrellas) / sizeof(umbrellas[0]);

sort(umbrellas, umbrellas + n, greater<int>());

排序函数似乎无法正常运行,当我运行代码时,它会生成错误。有没有办法从

{{5, 6}, {2, 7}, {9, 20}}

进入

{{2, 7}, {5, 6}, {9, 20}}

数组进行分类?

I am trying to sort a c++ subarray by first element.
My code is currently set up like this:

int umbrellas[3][2] = {{5, 6}, {2, 7}, {9, 20}};

int n = sizeof(umbrellas) / sizeof(umbrellas[0]);

sort(umbrellas, umbrellas + n, greater<int>());

The sort function doesn't seem to be functioning properly and when I run the code it generates errors. Is there a way to sort the array from

{{5, 6}, {2, 7}, {9, 20}}

into

{{2, 7}, {5, 6}, {9, 20}}

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

方圜几里 2025-02-19 12:14:21

使用std :: vector std :: vector当您的容器和排序变得更加容易使用时。不仅std :: vector C ++的首选容器,而且在其上使用STL功能更简单,直接,而没有任何实质性的开销。

现在定义数据,

std::vector<std::vector<int>> umbrellas{
    {5, 6},
    {2, 7},
    {9, 20}
};

您可以使用自定义比较器lambda,该lambda列入两个向量元素引用,并在上述矢量的第一个元素小于下面的元素时返回true。

std::sort(umbrellas.begin(),
          umbrellas.end(),
          [](const std::vector<int> &above, const std::vector<int> &below)
          {
              return (above[0] < below[0]);
          });

和输出:

for (auto &&row : umbrellas) {
    for (auto element : row) {
        std::cout<< element<< " ";
    }
    std::cout<< "\n";
}
2 7 
5 6 
9 20

将其带到C ++ 20,甚至更容易:

std::ranges::sort(umbrellas, std::less(),
     [](const auto &v) { return v[0];});

Use a std::vector of std::vector as your container and the sort becomes much easier to do with. Not only is std::vector the preferred container for C++ but using STL functions on it is way simpler and direct , without any substantiable overhead.

Define your data as

std::vector<std::vector<int>> umbrellas{
    {5, 6},
    {2, 7},
    {9, 20}
};

Now you can use a custom comparator lambda that takes in two vector element references and returns True when the first element of the above vector is smaller than that of the one below.

std::sort(umbrellas.begin(),
          umbrellas.end(),
          [](const std::vector<int> &above, const std::vector<int> &below)
          {
              return (above[0] < below[0]);
          });

And the output :

for (auto &&row : umbrellas) {
    for (auto element : row) {
        std::cout<< element<< " ";
    }
    std::cout<< "\n";
}
2 7 
5 6 
9 20

Taking this to C++20 it's even easier:

std::ranges::sort(umbrellas, std::less(),
     [](const auto &v) { return v[0];});
终止放荡 2025-02-19 12:14:21

如果时间复杂性无关紧要,则此代码将通过O(n^2)复杂性实现所需的时间

int arr[3][2] = {{5, 6}, {2, 7}, {9, 20}};

int n = sizeof(arr) / sizeof(arr[0]);

for(int i = 0 ; i < n - 1; i++){
    for(int j = 0 ; j < n - 1 ; j++){
        if(arr[j][0] > arr[j + 1][0])
            swap(arr[j],arr[j + 1]);
    }
}

If time complexity doesn't matter, this code will achieve the desired with O(n^2) complexity

int arr[3][2] = {{5, 6}, {2, 7}, {9, 20}};

int n = sizeof(arr) / sizeof(arr[0]);

for(int i = 0 ; i < n - 1; i++){
    for(int j = 0 ; j < n - 1 ; j++){
        if(arr[j][0] > arr[j + 1][0])
            swap(arr[j],arr[j + 1]);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文