Sorting 如何对多个列进行排序:CSV?c++;

Sorting 如何对多个列进行排序:CSV?c++;,sorting,csv,c++11,vector,stl-algorithm,Sorting,Csv,C++11,Vector,Stl Algorithm,我正试图通过指定排序的列顺序对CSV文件进行排序: 例如:./csort 3、1、5SORTED\u数据 或./c第3、4、6、2、1、5条

我正试图通过指定排序的列顺序对CSV文件进行排序:

例如:./csort 3、1、5SORTED\u数据

或./c第3、4、6、2、1、5条<数据

数据行示例:177,27,2,42285220

我使用了vector split(string str)函数来存储需要排序的参数中指定的列。创建向量:

vector<string> columns {3, 1, 5}; // for example

最简单的方法是使用一个具有索引列表的类作为成员,并遍历该列表以查看该项是否比另一项少

class VecLess
{
    std::vector<int> indexes;
public:
    VecLess(std::vector<int> init) : indexes(init)
    {
    }
    bool operator()(const std::vector<string> & lhs, const std::vector<string> rhs)
    {
        for (auto i = indexes.begin(); i != indexes.end(); ++i)
        {
            if (lhs[*i] < rhs[*i])
                return true;
            if (rhs[*i] < lhs[*i])
                return false;
        }
        return false;
    }
};
无向量类
{
std::向量索引;
公众:
无向量(std::vector init):索引(init)
{
}
布尔运算符()(常数std::vector&lhs,常数std::vector rhs)
{
for(自动i=索引.begin();i!=索引.end();++i)
{
如果(lhs[*i]
最简单的方法是使用一个具有索引列表的类作为成员,并遍历该列表,以查看该项是否小于另一项

class VecLess
{
    std::vector<int> indexes;
public:
    VecLess(std::vector<int> init) : indexes(init)
    {
    }
    bool operator()(const std::vector<string> & lhs, const std::vector<string> rhs)
    {
        for (auto i = indexes.begin(); i != indexes.end(); ++i)
        {
            if (lhs[*i] < rhs[*i])
                return true;
            if (rhs[*i] < lhs[*i])
                return false;
        }
        return false;
    }
};
无向量类
{
std::向量索引;
公众:
无向量(std::vector init):索引(init)
{
}
布尔运算符()(常数std::vector&lhs,常数std::vector rhs)
{
for(自动i=索引.begin();i!=索引.end();++i)
{
如果(lhs[*i]
最简单的方法是使用一个具有索引列表的类作为成员,并遍历该列表,以查看该项是否小于另一项

class VecLess
{
    std::vector<int> indexes;
public:
    VecLess(std::vector<int> init) : indexes(init)
    {
    }
    bool operator()(const std::vector<string> & lhs, const std::vector<string> rhs)
    {
        for (auto i = indexes.begin(); i != indexes.end(); ++i)
        {
            if (lhs[*i] < rhs[*i])
                return true;
            if (rhs[*i] < lhs[*i])
                return false;
        }
        return false;
    }
};
无向量类
{
std::向量索引;
公众:
无向量(std::vector init):索引(init)
{
}
布尔运算符()(常数std::vector&lhs,常数std::vector rhs)
{
for(自动i=索引.begin();i!=索引.end();++i)
{
如果(lhs[*i]
最简单的方法是使用一个具有索引列表的类作为成员,并遍历该列表,以查看该项是否小于另一项

class VecLess
{
    std::vector<int> indexes;
public:
    VecLess(std::vector<int> init) : indexes(init)
    {
    }
    bool operator()(const std::vector<string> & lhs, const std::vector<string> rhs)
    {
        for (auto i = indexes.begin(); i != indexes.end(); ++i)
        {
            if (lhs[*i] < rhs[*i])
                return true;
            if (rhs[*i] < lhs[*i])
                return false;
        }
        return false;
    }
};
无向量类
{
std::向量索引;
公众:
无向量(std::vector init):索引(init)
{
}
布尔运算符()(常数std::vector&lhs,常数std::vector rhs)
{
for(自动i=索引.begin();i!=索引.end();++i)
{
如果(lhs[*i]
根据我对您问题的理解,您已经将数据解析为4个向量,每列1个向量,您希望能够对数据进行排序,指定要排序的列的顺序--即按col1、col3、col4排序

你想做的事情并不太难,但你得回溯一下。有多种方法可以解决这个问题,但这里有一个粗略的概述。根据你在问题中表现出的专业水平,你可能需要在下面的提纲中看几个术语,但如果你这样做了,你将有一个很好的灵活的解决方案来解决你的问题

  • 您希望按行存储数据,因为您希望对行进行排序。。。4列的4个向量在这里对您没有帮助。如果行中的所有4个元素都将是同一类型,则可以对行使用std::vector或std::array。如果编译时已知#cols,则std::array为实数,运行时为std::vector。如果类型不一致,可以使用元组或结构。不管你用什么类型的,我们都叫它RowT

  • 解析并存储到行中,生成RowT的向量

  • 定义一个函数对象,该对象为RowT的左侧和右侧提供()运算符。它必须按照您想要的优先级实现“小于”操作。我们把这个类称为CustomSorter

  • 一旦你准备好了,你的最终分类将是:

    CustomSorter cs(/*precedence arguments*/);
    std::sort(rows.begin(), rows.end(), cs);
    

    一切都非常简单,在customsort示例中可以看到一个基本示例。根据我的经验,您需要处理的唯一部分是排序算法本身。

    我理解您的问题,您已经将数据解析为4个向量,每列1个向量,并且您希望能够对数据进行排序,指定要排序的列的顺序——即按col1、col3、col4排序

    你想做的事情并不太难,但你得回溯一下。有多种方法可以解决这个问题,但这里有一个粗略的概述。根据你在问题中表现出的专业水平,你可能需要在下面的提纲中看几个术语,但如果你这样做了,你将有一个很好的灵活的解决方案来解决你的问题

  • 您希望按行存储数据,因为您希望对行进行排序。。。4列的4个向量在这里对您没有帮助。如果行中的所有4个元素都将是同一类型,则可以对行使用std::vector或std::array。如果编译时已知#cols,则std::array为实数,运行时为std::vector。如果类型不一致,可以使用元组或结构。不管你用什么类型的,我们都叫它RowT

  • 解析并存储到行中,生成RowT的向量

  • 定义一个函数对象,该对象为RowT的左侧和右侧提供()运算符。它必须按照您想要的优先级实现“小于”操作。我们把这个类称为CustomSorter

  • 一旦你准备好了,你的最终分类将是:

    CustomSorter cs(/*precedence arguments*/);
    std::sort(rows.begin(), rows.end(), cs);
    
    一切都非常简单,在customsort示例中可以看到一个基本示例。根据我的经验,你唯一需要做的就是排序算法本身。

    据我所知