在 C++ 中对字符串进行排序使用运算符 <

发布于 2024-10-28 00:34:38 字数 173 浏览 6 评论 0 原文

以下数组以 C++ 代码给出:

char strings[105][105];

使用 STL sort 函数编写 operator< 来对字符串进行排序的正确方法是什么?有可能吗?

The following array is given in C++ code:

char strings[105][105];

What is the correct way to write operator< to sort the strings using STL sort function and is it possible at all?

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

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

发布评论

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

评论(4

任谁 2024-11-04 00:34:39

该代码实际上看起来很像 C 代码,而不是使用 std::string 的 C++ 代码。

没有办法编写一个可以与 std::sort 一起使用的 operator< ,因为除非你也编写它,否则没有任何交换可以正常工作。

使用 std::string 会使这变得非常简单,否则你必须编写自己的 operator< (查看 C 函数 strcmp ) 和交换函数。

编辑:请注意,交换 std::string 几乎肯定比交换 char 数组中的大量内存更快。

That code actually looks suspiciously like C code, not C++ which would use std::string.

There's no way to write an operator< that will work with std::sort because there's no swap that will work right unless you write that TOO.

Using std::string would make this pretty trivial, otherwise you'll have to write your own operator< (look at the C function strcmp) and swap functions.

EDIT: Note that swapping std::strings will almost certainly be faster than swapping huge swaths of memory in a char array.

情泪▽动烟 2024-11-04 00:34:39

不可能编写 operator< 来处理 char 数组。

It's not possible to write an operator< to work with char arrays.

戏剧牡丹亭 2024-11-04 00:34:39

假设您确实确实需要按行对二维数组进行排序,那么让 std::sort() 为您执行此操作有点困难,即使有一个有效的比较器函子:它需要某种迭代器适配器。

但是,您可以轻松使用其他就地排序算法,例如选择排序:

#include <iostream>
#include <algorithm>
#include <string>

template<int N>
bool char_array_less(const char(&l)[N], const char(&r)[N])
{
   return std::char_traits<char>::compare(&l[0], &r[0], N) < 0;
// for a more general solution
// return std::lexicographical_compare(&l[0], &l[0]+N, &r[0], &r[0]+N);
}

template<int N>
void swap_char_arrays( char(*l)[N], char(*r)[N])
{
    std::swap_ranges(&(*l)[0], &(*l)[0]+N, &(*r)[0]);
}

const int ROWS = 105;
const int COLS = 105;
int main()
{
    char a[ROWS][COLS] = {"foo", "bar", "whatever" };

    for(char(*i)[COLS] = a; i != a+ROWS; ++i)
        swap_char_arrays(i,
                         std::min_element(i, a+ROWS, char_array_less<COLS>));

    for(int i=0; i<ROWS; ++i)
        std::cout << a[i] << '\n';
}

测试运行: https://ideone.com/ 15小时RB

Assuming you really do need to sort a 2D array row-wise, it's a bit difficult to make std::sort() do this for you, even given a working comparer functor: it would need some sort of iterator adapter.

However, you can easily use other in-place sorting algorithms, such as selection sort:

#include <iostream>
#include <algorithm>
#include <string>

template<int N>
bool char_array_less(const char(&l)[N], const char(&r)[N])
{
   return std::char_traits<char>::compare(&l[0], &r[0], N) < 0;
// for a more general solution
// return std::lexicographical_compare(&l[0], &l[0]+N, &r[0], &r[0]+N);
}

template<int N>
void swap_char_arrays( char(*l)[N], char(*r)[N])
{
    std::swap_ranges(&(*l)[0], &(*l)[0]+N, &(*r)[0]);
}

const int ROWS = 105;
const int COLS = 105;
int main()
{
    char a[ROWS][COLS] = {"foo", "bar", "whatever" };

    for(char(*i)[COLS] = a; i != a+ROWS; ++i)
        swap_char_arrays(i,
                         std::min_element(i, a+ROWS, char_array_less<COLS>));

    for(int i=0; i<ROWS; ++i)
        std::cout << a[i] << '\n';
}

test run: https://ideone.com/15hRB

小梨窩很甜 2024-11-04 00:34:39

您不能重载指针的 operator<,但也不需要这样做,因为 std::sort 可以接受任何比较函数(或仿函数)。

另一个问题是排序算法无法交换数组,因为它们不可分配。但是您可以将指针数组排序到二维数组中(保持原始数组不变)。

#include <algorithm>
#include <cstring>
#include <cstdio>

bool compare_cstring(const char* a, const char* b)
{
    return strcmp(a, b) < 0;
}

int main()
{
    const int count = 5;
    char strings[count][10] = { "One", "Two", "Three", "Four", "Five" };
    char* sorted_view[count];
    for (int i = 0; i != count; ++i) {
        sorted_view[i] = strings[i];
    }

    std::sort(sorted_view, sorted_view + count, compare_cstring);
    for (int i = 0; i != count; ++i) {
        puts(sorted_view[i]);
    }
}

You can't overload operator< for pointers, but you don't need to, since std::sort can accept any comparison function (or functor).

Another problem is that the sort algorithm cannot swap arrays, because they are not assignable. But you can sort an array of pointers into the two-dimensional array (leaving the original array as it is).

#include <algorithm>
#include <cstring>
#include <cstdio>

bool compare_cstring(const char* a, const char* b)
{
    return strcmp(a, b) < 0;
}

int main()
{
    const int count = 5;
    char strings[count][10] = { "One", "Two", "Three", "Four", "Five" };
    char* sorted_view[count];
    for (int i = 0; i != count; ++i) {
        sorted_view[i] = strings[i];
    }

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