Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Q基于c字符串中的列进行排序?_C++_C_Qsort - Fatal编程技术网

C++ Q基于c字符串中的列进行排序?

C++ Q基于c字符串中的列进行排序?,c++,c,qsort,C++,C,Qsort,类项目涉及对字符串数组进行排序,每个字符串包含相同数量的列,如下所示: Cartwright Wendy 93 Williamson Mark 81 Thompson Mark 100 Anderson John 76 Turner Dennis 56 我的问题是qsort作为arg使用的比较函数指针。如果我理解正确,比较函数必须使用两个const void指针指向要排序的项,并返回一个int。这意味着我不能将指向结构的指针传

类项目涉及对字符串数组进行排序,每个字符串包含相同数量的列,如下所示:

Cartwright Wendy 93 Williamson Mark 81 Thompson Mark 100 Anderson John 76 Turner Dennis 56 我的问题是qsort作为arg使用的比较函数指针。如果我理解正确,比较函数必须使用两个const void指针指向要排序的项,并返回一个int。这意味着我不能将指向结构的指针传递到比较函数中,因为qsort将不进行排序。我无法将列号传递给比较函数进行排序,因为它只能接受两个参数。如何根据特定列对这些字符串进行排序?

编辑:排序仅限于qsort或我自己的,如果我真的想要的话。给出选择,我选择qsort.:)


编辑#2:共识似乎是要么使用全局变量作为列号,要么只使用qsort对结构数组进行排序。我没有想到只对结构进行排序,并使用其中的指针打印出原始字符串。我想这就是我要做的。谢谢大家的帮助

假设您不局限于使用qsort,您可以使用std::sort和存储列号的functor对象。如果必须使用qsort,一个快速而肮脏的解决方案是将列号存储在全局变量中,并在Comparison函数中使用它。

不同的comparator函数,所有这些函数都采用整个结构,但每个函数只使用一列进行比较。

C++还是C++?基于你的标签,我假设它是C++。让我们试试STL方式

您应该使用
std::sort
而不是
qsort
std::sort
不仅可以使用函数指针(与其C替代方案相比),还可以使用任何可以作为函数调用的对象。您可能知道,类实例可以通过
operator()
作为函数调用。然后,解决方案很简单:创建一个“functor”类,该类将在构造时创建不同的函数。然后,排序调用将如下所示:

std::sort(array, array+size, comparator(2 /* sort by column #2 */));
functor类有效地创建了一个所谓的“闭包”:一个动态创建的函数对象,具有局部变量,但不与以这种方式创建的其他函数对象共享它们。它看起来是这样的:

struct line {
    char * line;
    char column_to_sort_on[MAX_COLUMN];
}
class comparator{
  private: unsigned int field_n;
  public: comparator(unsigned int _field_n) : field_n(_field_n) {};
  public: int operator () (char const *  lhs, char const * rhs)
       { /* compare here fields with index field_n */ };
};
struct line {
    char * line;
    char column_to_sort_on[MAX_COLUMN];
}
...

line*  Lines[max_lines]; // here you store the structs

int
cmp_lines( const void *elem1, const void *elem2 )
{
    line*  line1 = *(line**)elem1;
    line*  line2 = *(line**)elem2;
    // do the comparisons
}

qsort(Lines, max_lines, sizeof(line*), cmp_lines);
请注意,比较“function”(即您创建的类实例)没有空指针,而是具有
char*
参数,因此您不必为类型转换而烦恼


不幸的是,在C语言中,除了创建一个全局变量之外,您不能用其他方法来实现这一点。

您可以像这样传递结构:

struct line {
    char * line;
    char column_to_sort_on[MAX_COLUMN];
}
class comparator{
  private: unsigned int field_n;
  public: comparator(unsigned int _field_n) : field_n(_field_n) {};
  public: int operator () (char const *  lhs, char const * rhs)
       { /* compare here fields with index field_n */ };
};
struct line {
    char * line;
    char column_to_sort_on[MAX_COLUMN];
}
...

line*  Lines[max_lines]; // here you store the structs

int
cmp_lines( const void *elem1, const void *elem2 )
{
    line*  line1 = *(line**)elem1;
    line*  line2 = *(line**)elem2;
    // do the comparisons
}

qsort(Lines, max_lines, sizeof(line*), cmp_lines);

我想把它存储在一个全局变量中,但如果可能的话,我想避免它。这个比较器函数不需要对一个结构数组进行排序吗?我认为我传入comparator函数的内容必须与qsort排序的内容相同,我只想对原始字符串数组进行排序。@Jergason,我纠正了代码中的一个错误(line1和line2值)。赋值是说必须将字符串数组传递给qsort,还是说必须对字符串数组进行排序,不管付出什么代价?如果是后者,则可以对结构数组进行排序,然后根据结果对字符串数组重新排序。