在C ++中排序字符串向量的向量

时间:2011-08-18 21:37:02

标签: c++ sorting vector

我很难弄明白,如何对字符串向量的矢量进行排序,这是测试代码。


#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>

int main(int argc, char** argv) {
  std::vector <std::vector <std::string> > data_var;
  std::vector <std::string> temp;

  std::string str1 = "1,hello3,temp2";
  std::string str2 = "2,hello2,temp1";
  std::string str3 = "3,hello1,temp3";

  boost::split(temp, str1, boost::is_any_of(","));
  data_var.push_back(temp);
  boost::split(temp, str2, boost::is_any_of(","));
  data_var.push_back(temp);
  boost::split(temp, str3, boost::is_any_of(","));
  data_var.push_back(temp);

  // sorting code here...
}

提前致谢...

4 个答案:

答案 0 :(得分:5)

好吧:新的 - 简单 - 回答,了解到矢量具有可比性:

//sorting code here...
std::sort(data_var.begin(), data_var.end(), std::greater<std::vector<std::string>>());

答案 1 :(得分:1)

我将假设每个向量代表某种类型的记录,并从左到右比较内部字符串。显然sorter()代码很容易替换。您应该在代码的某处添加sorter()函数,并将其传递给std :: sort算法。

bool sorter(const std::vector<std::string>& left, const std::vector<std::string>& right)
{
    //go through each column
    for(int i=0; i<left.size() && i<right.size()) {
        // if left is "more" return that we go higher
        if( left[i] > right[i])
            return true;
        // if left is "less" return that we go lower
        else if (left[i] < right[i])
            return false;
    }
    // if left is longer, it goes higher
    if (left.size() > right.size())
        return true;
    else //otherwise, left go lower
        return false;
 }

 int main() {
     std::vector <std::vector <std::string> > data_var;
     //...

     //sorting code here...
     std::sort(data_var.begin(), data_var.end(), sorter);

     //...
 }

答案 2 :(得分:1)

如果您只想根据第二列进行排序,那么您只需要提供自定义比较运算符。一旦这样做是:

struct StringListCompare
{
  bool operator()(const vector<string>& lhs, const vector<string>& rhs)
  {
    // what do we do if lhs or rhs don't have two elements?
    if (lhs.size() < 2 || rhs.size() < 2)
    {
      // ?
    }
    else
    {
      return lhs[1] < rhs[1];
    }
  }
} StringListComparer;

int main()
{
  // ...
  sort(data_var.begin(), data_var.end(), StringListComparer);
}

编辑:如果您在运行时之前不知道要对哪个列进行排序,则可以在排序对象中对其进行编码:

class StringListCompare
{
public:
  explicit StringListCompare(int column) : m_column(column) {}
  bool operator()(const vector<string>& lhs, const vector<string>& rhs)
  {
    // what do we do if lhs or rhs don't have (m_column + 1) elements?
    return lhs[m_column] < rhs[m_column];
  }
private:
  int m_column;
};

注意我们是如何添加一个构造函数来获取它所依赖的列。您可以像这样使用它:

  // We set it up so the columns are 0-based:
  StringListCompare compare_column_0(0), compare_column_1(1), compare_column_2(2);

  cout << "Original:\n" << data_var << endl;
  sort(data_var.begin(), data_var.end(), compare_column_2);
  cout << "Sorted on column 2:\n" << data_var << endl;
  sort(data_var.begin(), data_var.end(), compare_column_1);
  cout << "Sorted on column 1:\n" << data_var << endl;
  sort(data_var.begin(), data_var.end(), compare_column_0);
  cout << "Sorted on column 0:\n" << data_var << endl;

如果您不想:

,甚至不需要创建局部变量
  sort(data_var.begin(), data_var.end(), StringListCompare(2));
  cout << "Sorted on 2, no local sort variable:\n" << data_var << endl;

[Code at ideone]

答案 3 :(得分:0)

从算法中查看排序函数:

template <class RandomAccessIterator> void sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare> void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

Click here for an example + docs