sorting vector of vector of strings in C++

后端 未结 4 620
既然无缘
既然无缘 2020-12-21 01:23

I am having trouble to figure out, how to sort a vector of vector of strings, here is the testing code.


#include <iostream>
#include <vector>
#incl         


        
4条回答
  •  悲哀的现实
    2020-12-21 01:40

    I'm going to assume each vector represents an record of some type, and compare the internal strings from left to right. Obviously the sorter() code is easily replaceable. You should to add a sorter() function somewhere to your code, and pass it to the std::sort algorithm.

    bool sorter(const std::vector& left, const std::vector& right)
    {
        //go through each column
        for(int i=0; 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  > data_var;
         //...
    
         //sorting code here...
         std::sort(data_var.begin(), data_var.end(), sorter);
    
         //...
     }
    

提交回复
热议问题