【问题标题】:C++ Sort a vector of vector of strings with comparator. Please help me understandC++ 使用比较器对字符串向量的向量进行排序。请帮我理解
【发布时间】:2013-03-03 12:45:35
【问题描述】:

我正在尝试对字符串向量的向量进行排序,但我不明白如何创建比较器函数。

我看到了这个线程,但无法在我的情况下实现: sorting vector of vector of strings in C++

所以我有一个字符串向量的向量,如下所示:
你好,世界,1、3、4、7、2、1
世界,你好,1, 4, 8, 4 ,2, 1
手机、鼠标、2、3、5、2、1、4

我需要按我的用户指定的列对这个向量字符串向量进行排序。我的用户可以指定多个列进行排序。假设第 3 列和第 5 列。第 3(1,1,2) 列的第 1 行和第 2 行具有相同的值,那么我们必须按第 5 列排序。为了不复杂,这都是按升序排列的。

当您将它传递给比较器函数时,我不明白它是如何工作的概念。我的函数如何在这些线程中的人发布的示例中循环?

无论如何提前谢谢!

【问题讨论】:

  • stackoverflow.com/questions/15183953/…(今天其他用户提出同样的问题)
  • 我知道这个 lhs 和 rhs 的东西,但我不知道如何初始化 lhs 和 rhs 向量。应该是什么?我在 vector > allInputs;. 中有我所有的字符串向量

标签: c++


【解决方案1】:

您可以只使用std::sort 对向量进行排序,并定义一个自定义比较函子(即具有重载operator() 的类)。

您可以将排序列的索引存储在 std::vector(这将是自定义比较器对象的“状态”的一部分)中,并比较索引存储在该向量中的列中的字符串。

您开始比较“排序列”向量中第一个索引中指定的列的值;如果它们相同,则继续比较向量中下一个索引中指定的列的值,等等。这可以在比较器的operator() 重载主体内的for 循环内完成。

以以下代码为例(使用g++(GCC)4.7.2编译):

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

vector<vector<string>> BuildTestData()
{   
    vector<string> r1 = {"hello", "world", "1", "3", "4", "7", "2", "1"};
    vector<string> r2 = {"world", "hello", "1", "4", "8", "4", "2", "1"};
    vector<string> r3 = {"phone", "mouse", "2", "3", "5", "2", "1", "4"};

    return vector<vector<string>>{r1, r2, r3};
}

void PrintData(const vector<vector<string>> & v)
{
    for (size_t r = 0; r < v.size(); r++)
    {
        for (size_t c = 0; c < v[r].size(); c++)
            cout << v[r][c] << ' ';
        cout << '\n';
    }
}

class StringListComparator
{
public:
    explicit StringListComparator(vector<int> sortColumns)
        : m_sortColumns( move(sortColumns) )
    {
    }

    bool operator()(const vector<string>& lhs, const vector<string>& rhs) const
    {   
        // For each sorting column:
        for (size_t i = 0; i < m_sortColumns.size(); i++)
        {
            // Comparison with current column
            const int currentColumn = m_sortColumns[i];

            if (lhs[currentColumn] < rhs[currentColumn])
                return true;

            if (lhs[currentColumn] > rhs[currentColumn])
                return false;

            // lhs[currentColumn] == rhs[currentColumn],
            // so check with next sorting column
        }

        return false;
    }

private:
    vector<int> m_sortColumns;
};

int main()
{
    auto v = BuildTestData();
    cout << "Before sorting:\n";    
    PrintData(v);

    vector<int> sortColumns = {5, 7}; // indexes are 0-based

    sort(v.begin(), v.end(), StringListComparator(sortColumns));

    cout << "\nAfter sort:\n";
    PrintData(v);
}

示例运行:

Before sorting:
hello world 1 3 4 7 2 1
world hello 1 4 8 4 2 1
phone mouse 2 3 5 2 1 4

After sort:
phone mouse 2 3 5 2 1 4
world hello 1 4 8 4 2 1
hello world 1 3 4 7 2 1

【讨论】:

  • 非常感谢。我现在明白了。谢谢!!
  • @user1375155:做一个优秀的 StackOverflow 公民:如果你觉得这篇文章(以及其他人写的文章)有用,请给他们投票并将最好的标记为答案。
猜你喜欢
  • 1970-01-01
  • 2015-09-28
  • 2023-03-18
  • 2020-02-19
  • 1970-01-01
  • 2020-02-22
  • 2011-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多