Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 根据优先级按字典顺序对向量列表进行排序_C++_List_Sorting_Vector_Lexicographic - Fatal编程技术网

C++ 根据优先级按字典顺序对向量列表进行排序

C++ 根据优先级按字典顺序对向量列表进行排序,c++,list,sorting,vector,lexicographic,C++,List,Sorting,Vector,Lexicographic,假设我有一个字符串向量列表: [“a”、“c”、“duck”] [“a”、“a”、“f”] [“蜜蜂”、“s”、“xy”] [“b”、“a”、“a”] 我想用这种方式对向量进行排序: 首先,根据索引0处的元素按字典顺序进行排序,如果存在并列关系,则将根据索引1处的元素按字典顺序进行排序,如果存在其他并列关系,则将根据索引2处的元素按字典顺序进行排序 因此,上述列表在排序后如下所示: [“a”、“a”、“f”] [“a”、“c”、“duck”] [“b”、“a”、“a”] [“蜜蜂”、“s”、“x

假设我有一个字符串向量列表:

[“a”、“c”、“duck”]

[“a”、“a”、“f”]

[“蜜蜂”、“s”、“xy”]

[“b”、“a”、“a”]

我想用这种方式对向量进行排序:

首先,根据索引0处的元素按字典顺序进行排序,如果存在并列关系,则将根据索引1处的元素按字典顺序进行排序,如果存在其他并列关系,则将根据索引2处的元素按字典顺序进行排序

因此,上述列表在排序后如下所示:

[“a”、“a”、“f”]

[“a”、“c”、“duck”]

[“b”、“a”、“a”]

[“蜜蜂”、“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;

}
bool CompareVector(向量第一,向量第二){
if(第一个[0]<第二个[0])
返回true;
如果(第一[1]<第二[1])
返回true;
如果(第一[2]<第二[2])
返回true;
返回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;
bool CompareVector(向量第一,向量第二){
对于(int i=0;i
}

然后我可以调用标准排序函数:

sort(vector<vector<string> >input.begin(), vector<vector<string> >input.end(), CompareVector() )
排序(vectorinput.begin()、vectorinput.end()、CompareVector())

这样行吗?谢谢。

只要打电话给
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;
}

这将是一个示例实现:

#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;
}
#包括
#包括
typedef std::vector StringTuple;
typedef std::向量MyList;
bool mySort(常量StringTuple&a、常量StringTuple&b)
{
如果(a[0]==b[0]){
如果(a[1]==b[1]){
返回a[2]std::cout非常简单。您需要创建符号的虚拟质量,并且需要为每个符号设置它们的索引号,从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
       }
   }
}
std::vector m_vector;
std::string abc=“abcde…”;//此变量有一个字母表
void f()
{
对于(int i=0;i符号)
{
//前进
}
}
}

另外,您还需要临时存储。

首先,您需要一个用于
std::string
的自然排序比较器。然后这很简单。我的意思是,我不想重写排序算法,比如说,合并排序,因为它已经内置。但不知何故,我想在我的方法中实现它。我的意思是,我可能需要定义vec的顺序然后我可以调用sort()通过传递排序从标准库中获取函数。但是如何在代码中定义排序?向量的长度不一定总是3。但是所有向量的长度都是相同的。非常好地使用了
auto
和新的for iterator。我应该用这些特性更新我的示例。你能解释一下吗,h循环的底部如何工作并打印向量的元素
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
       }
   }
}