2014-03-03 128 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 
     } 
    } 
} 

此外,您需要臨時存儲。