2014-03-03 120 views
0

说我有字符串的向量的列表:排序向量的列表字典顺序根据优先级

[“一”,“C”,“鸭”]

[“一”,“一个”, “F”]

[ “蜂”, “S”, “XY”]

[ “b”, “一”, “一个”]

欲向量排序通过这种方式:

首先按照索引0处的元素按照字典顺序排序,如果存在联系,则将按照索引1处的元素按字典顺序确定,如果存在另一个联系,则将按照元素的字典顺序确定在索引2

所以上面的列表将被排序之后,如下所示:

[ “一”, “一个”, “F”]

[ “一”, “C”, “duck”]

[“b”,“a”,“a”]

[“bee”,“s”,“xy”]

如何根据上面的描述来实现标准库sort()函数来编写一个向量列表的排序方法?我正在使用C++。 谢谢。

一旦知道每个向量的长度,就不难编写比较函数。但是如果我不知道向量的长度(但我总是知道它们的长度相同)呢? 的长度为3的向量比较功能:

bool CompareVector(vector<string> first, vector<string> second){ 
    if (first[0] < second[0]) 
     return true; 
    if (first[1] < second[1]) 
     return true; 
    if (first[2] < second[2]) 
     return true; 
    return false; 

} 

因此,对于长度为n的向量,将有N个if语句。但是,我怎样才能保持if语句的数量变量?

如何:

bool CompareVector(vector<string> first, vector<string> second){ 
    for (int i=0; i< first.size(); i++) 
     if (first[i] < second[i]) 
     return true; 
    return false; 

}

然后我就可以调用标准排序功能:

sort(vector<vector<string> >input.begin(), vector<vector<string> >input.end(), CompareVector()) 

将这项工作?谢谢。

+0

首先,你需要一个用于'std :: string'的自然排序比较器。那很简单。 –

+0

我的意思是我不想重写排序算法,比如合并排序,因为它已经内置了。但不知何故,我想在我的方法中实现它。 – user3213711

+0

我的意思是,我可能需要定义向量的排序。然后我可以通过传入顺序来调用标准库中的sort()函数。但是,我怎样才能定义代码中的顺序?矢量的长度不一定总是3.但是所有矢量的长度都是相同的。 – user3213711

回答

5

只需拨打std::sort就可以做正确的事情。它执行矢量的每个元素的字典对比,并且这是递归的。

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

int main() 
{ 
    std::vector<std::vector<std::string>> v{{"a", "c", "duck"}, 
              {"a", "a", "f"}, 
              {"bee", "s", "xy"}, 
              {"b", "a", "a"}}; 
    std::sort(v.begin(), v.end()); 

    for (const auto& v_: v) 
    { 
    for (const auto& s : v_) 
     std::cout << s << " "; 
    std::cout << std::endl; 
    } 
    std::cout << std::endl; 
} 

输出:

a a f 
a c duck 
b a a 
bee s xy 
+1

“auto”和迭代器的新用法非常好用。我应该更新我的示例以及这些功能。 – Flovdis

+0

ahaha))你是对的!我忘了这件事。 – Ivan

+0

你能解释一下,循环的底部是如何工作的并且打印矢量的元素 – aash20

0

这将是一个示例实现:

#include <iostream> 
#include <vector> 

typedef std::vector<std::string> StringTuple; 
typedef std::vector<StringTuple> MyList; 

bool mySort(const StringTuple &a, const StringTuple &b) 
{ 
    if (a[0]==b[0]) { 
     if (a[1]==b[1]) { 
      return a[2] < b[2]; 
     } else { 
      return a[1] < b[1]; 
     } 
    } else { 
     return a[0] < b[0]; 
    } 
} 

void showList(const std::vector<StringTuple> &list) 
{ 
    for (MyList::const_iterator it = list.begin(); it != list.end(); ++it) { 
     const StringTuple &tuple = *it; 
     for (StringTuple::const_iterator it2 = tuple.begin(); it2 != tuple.end(); ++it2) { 
      std::cout << "\t\"" << *it2 << "\""; 
     } 
     std::cout << std::endl; 
    } 
} 


int main(int argc, const char * argv[]) 
{ 
    MyList listToSort; 

    listToSort.push_back({"a", "c", "duck"}); 
    listToSort.push_back({"a", "a", "f"}); 
    listToSort.push_back({"bee", "s", "xy"}); 
    listToSort.push_back({"b", "a", "a"}); 

    std::cout << "Before sort:" << std::endl; 
    showList(listToSort); 
    std::sort(listToSort.begin(), listToSort.end(), mySort); 
    std::cout << "After sort:" << std::endl; 
    showList(listToSort); 
    return 0; 
} 
0

很简单。您需要创建虚拟的符号块,并且您需要为每个符号设置其索引编号,从1到N,并比较这些索引。 像这样:

std::vector<std::string> m_vector; 
std::string abc = "abcde....."; // this variable have an alphabet 

void f() 
{ 
    for(int i = 0; i < m_vector.size(); i++) 
    { 
     int symbol = 0; 
     for(int j = 0; j < abc.length(); j++) 
     { 
      //second index zero because we need to get a first symbol 
      if(m_vector[i][0] == abc[j]) 
      { 
       symbol = j; 
       break; 
      } 
     } 

     //here your comparison, as you need 
     //like this 
     if(prevItem > symbol) 
     { 
      //to move forward 
     } 
    } 
} 

此外,您需要临时存储。