Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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+中的向量进行排序+;_C++_Sorting_Vector - Fatal编程技术网

C++ 对c+中的向量进行排序+;

C++ 对c+中的向量进行排序+;,c++,sorting,vector,C++,Sorting,Vector,我有以下数据结构: std::vector<std::pair <std::vector<unsigned>,std::vector<unsigned> > > A; 现在我只想按照下面的方式对A的向量对中的第一个向量进行排序。例如,A的向量对中的第一个向量是: (7), (3), (9), (5), (8), (8,2,10), (5,7), (3,9), (10), (3,5), (3,7), (3,10), (3,4,5) 我想根据第一个

我有以下数据结构:

std::vector<std::pair <std::vector<unsigned>,std::vector<unsigned> > > A;
现在我只想按照下面的方式对A的向量对中的第一个向量进行排序。例如,A的向量对中的第一个向量是:

(7),
(3),
(9),
(5),
(8),
(8,2,10),
(5,7),
(3,9),
(10),
(3,5),
(3,7),
(3,10),
(3,4,5)
我想根据第一个向量对A进行排序,这样在最终排序后,我的向量变成:

((3),(100,101)),
((5),(102,103)),
((7),(108,109)),
((8),(110)),
((9),(111,112)),
((10),(114)),
((3,5),(115)),
((3,7),(118)),
((3,9),(119)),
((3,10),(120)),
((5,7),(121)),
((3,4,5),(122)),
**((2,8,10),(189)).**

我知道如何使用STD:So排序来分类向量,但我不知道如何使用标准C++函数对向量向量进行排序。我尝试先按大小对它们进行排序,然后使用bublee排序进行最终排序。是否有其他的方法来使用标准库函数在C++中对这些向量进行排序。我在Ubuntu 12.04上使用C++编译器(G++(Ubuntu/LimARO4.4.3-1Ubuntu5)4.63.)运行C++。
  • 对中第一个
    向量的大小进行第一次排序
  • 然后按字典顺序对
    向量进行排序
  • 您必须为此编写自己的比较器函数

    代码:

    bool mySort(const pair<vector<unsigned>,vector<unsigned> > &a , const pair<vector<unsigned>,vector<unsigned> > &b)
    {
        if (a.first.size() == b.first.size()) {
            //If sizes of the vectors are equal
            //Sort the graph lexicographically. 
            return std::lexicographical_compare(a.first.begin(),a.first.end(),b.first.begin(),b.first.end());pair<vector<unsigned>,vector<unsigned> > a
        } else {
            //Sort by size.
            return a.first.size() < b.first.size();
        }
    }
    int main()
    {
        std::vector<std::pair<std::vector<unsigned>,std::vector<unsigned> > > a;
        std::sort(a.begin(),a.end(),mySort);
    }
    
    bool mySort(常量对&a,常量对&b)
    {
    如果(a.first.size()==b.first.size()){
    //如果向量的大小相等
    //按字典顺序对图表排序。
    返回std::lexicographical_compare(a.first.begin()、a.first.end()、b.first.begin()、b.first.end());对a
    }否则{
    //按大小排序。
    返回a.first.size()
    哦,很抱歉,我可能误解了“词典编纂”一词的含义。我只是想确定你的问题是否清楚。你可以回答这个问题,尽管我没有一个好的术语来形容你所描述的那种。
    bool mySort(const pair<vector<unsigned>,vector<unsigned> > &a , const pair<vector<unsigned>,vector<unsigned> > &b)
    {
        if (a.first.size() == b.first.size()) {
            //If sizes of the vectors are equal
            //Sort the graph lexicographically. 
            return std::lexicographical_compare(a.first.begin(),a.first.end(),b.first.begin(),b.first.end());pair<vector<unsigned>,vector<unsigned> > a
        } else {
            //Sort by size.
            return a.first.size() < b.first.size();
        }
    }
    int main()
    {
        std::vector<std::pair<std::vector<unsigned>,std::vector<unsigned> > > a;
        std::sort(a.begin(),a.end(),mySort);
    }