【问题标题】:C++ vector<Struct> sorting not workingC++ vector<Struct> 排序不起作用
【发布时间】:2026-02-19 08:45:01
【问题描述】:

我目前正在尝试编写一个函数来对条目项的向量进行排序,这些项在我的头文件中定义。

struct Entry{
    string word;
    int count;
};

基本上,每个条目都有一个string 和一个int。我要做的是按每个条目的count 值按降序对vector&lt;Entry&gt; 进行排序。我尝试在 .cpp 文件中使用 std::sort

bool intcomp(const Entry &lhs, const Entry &rhs){
    return lhs.count < rhs.count;
}

void SortedByCount(std::ostream &out) const{
    std::sort(vocabulary.begin(), vocabulary.end(), intcomp);
}

但是编译器随后会吐出一堵巨大的错误墙,比如这个

/usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/bits/stl_heap.h:247:12: note:
  in instantiation of function template specialization
  'std::__push_heap<__gnu_cxx::__normal_iterator<Entry *const *,
  std::vector<Entry *, std::allocator<Entry *> > >, long, Entry *>'
  requested here
  std::__push_heap(__first, __holeIndex, __topIndex,
       ^

我很不知道该怎么做,所以任何指针都将不胜感激。

编辑: 头文件包含Entry的结构体及其构造函数,以及intcompSortedByCount(std::ostream &amp;out) const的原型,而.cpp文件包含intcompSortedByCount(std::ostream &amp;out) const的定义。我收到此错误:

 reference to non-static member function must
 be called
 std::sort(vocabulary.begin(), vocabulary.end(), intcomp);
                                                 ^

是因为intcomp() 方法不是静态的吗?或者还能是什么?再次感谢。

【问题讨论】:

  • 从错误信息可以看出你的vocabulary向量大概存储了指向Entry的指针,即std::vector&lt;Entry*&gt;
  • 你应该发一个MCVE

标签: c++ sorting vector


【解决方案1】:

您的vocabulary 向量包含指向Entrys 的指针,而不是Entrys 本身。将比较器更改为以下内容:

bool intcomp(const Entry *lhs, const Entry *rhs){
    return lhs->count < rhs->count;
}

【讨论】:

  • 我按照你说的做了,但它仍然吐出一堵墙,上面写着error: read-only variable is not assignable,还有这个note: in instantiation of function template specialization 'std::sort&lt;__gnu_cxx::__normal_iterator&lt;Entry *const *, std::vector&lt;Entry *, std::allocator&lt;Entry *&gt; &gt; &gt; &gt;' requested
  • 如果vocabulary 是与SortedByCount 相同类的成员,则删除const 声明末尾的const -- const 方法不能更改结构的状态。否则请确认vocabulary 本身不是const vector(尽管它似乎不是来自您粘贴的错误消息)
【解决方案2】:

工作得很好:

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

struct Entry {
    std::string word;
    int count;

    Entry(const std::string& s, int c) : word(s), count(c) {}
};

std::vector<Entry> vocabulary;

bool intcomp(const Entry &lhs, const Entry &rhs) {
    return lhs.count < rhs.count;
}

void sortedbycount(std::ostream &out) {
    std::sort(vocabulary.begin(), vocabulary.end(), intcomp);
}

void main()
{
    vocabulary.push_back(Entry("second", 2));
    vocabulary.push_back(Entry("firs", 1));
    vocabulary.push_back(Entry("third", 3));

    sortedbycount(std::cout);

    for (const auto& e : vocabulary)
    {
        std::cout << e.word << " " << e.count << "\n";
    }
}

【讨论】:

  • 这很奇怪,因为我收到了一个 reference to non-static member function must be called 错误,其中有一个小胡萝卜指向 std::sort 中对 intcomp 的调用。
【解决方案3】:

要使用自定义条件对矢量内容进行排序,您可以使用 C++11 lambda:

sort(entries.begin(), entries.end(),
    [](const Entry& a, const Entry& b)
    {
        return a.Count > b.Count;
    }
);

可编译源代码示例如下 (live here on Ideone):

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Entry 
{
    string Word;
    int Count;

    Entry(const string& word, int count)
        : Word(word), Count(count)
    {
    }
};

int main() {
    vector<Entry> entries = {{"hello", 2}, {"world", 8}, {"hi", 20}, {"connie", 10}};

    sort(entries.begin(), entries.end(),
        [](const Entry& a, const Entry& b)
        {
            return a.Count > b.Count;
        }
    );

    for (const auto& e : entries)
    {
        cout << e.Word << ": " << e.Count << '\n';
    }
}

输出:

hi: 20
connie: 10
world: 8
hello: 2

【讨论】: