用 C++ 对矢量排序

介绍

在本教程中,我们将专注于**在C++中排序一个矢量。

Sorting是任何编程语言中执行的操作之一,同样,在 C++中也有几种算法,我们可以按照这些算法对任何数据结构进行排序。

对于 [矢量]( / 社区 / 教程 / 矢量-in-c-plus-plus),我们可以以任何顺序执行分类操作(上升或下降)。

C++ 中的 Vector 以上升顺序排序

一个 [在C++中的矢量]( / 社区 / 教程 / 矢量-in-c-plus-plus)可以很容易地使用在算法头文件中定义的sort()函数以上升顺序进行排序。

sort() 函数对数据结构进行分类,不会返回任何东西. 分类发生在两个经过的迭代器或位置之间. 第三个参数决定了元素将比较的 **顺序。

默认情况下,如果不通过第三个参数,则该函数将其视为 std::less<int>() 函数. 该函数会根据比较两个参数而返回 truefalse,无论第一个参数小于另一个参数。

所以,现在让我们看看我们如何在C++(上升顺序)中排序矢量。

 1#include<iostream>    
 2#include<vector> 
 3#include<algorithm>
 4using namespace std;
 5int main()
 6{  
 7    //vector initialisation
 8    vector<int> vec {5, 4, 3, 2, 1};
 9    
10    cout<<"Before sorting vector : ";
11    for(auto i=vec.begin(); i<vec.end(); i++)
12    {
13    	cout<<" "<<*i;
14    }
15    
16    std::sort(vec.begin(),vec.end());//Sorting the vector
17    
18    cout<<"\n\nAfter sorting vector : ";
19    for(auto i=vec.begin(); i<vec.end(); i++)
20    {
21    	cout<<" "<<*i;
22    }
23    return 0;
24}

出发点:**

1Before sorting vector :  5 4 3 2 1
2
3After sorting vector :  1 2 3 4 5

在 C++ 中以下行顺序排序一个矢量

正如我们之前所说的,C++中的sort()函数的第三个参数决定了排序的顺序,因此,我们可以定义其中的函数以在我们想要的顺序中排序任何矢量(在这种情况下,下降)。

使用大() 在 sort() 中使用

类似于较小<int>()函数,较大()`函数将 bool 值返回为 true 或 false 但在 ** opposite** 的意义上. 如果第一个参数大于第二个参数,则函数将返回 true 和 false 如果上述条件是 false。

让我们看看我们如何使用它来以下降顺序获取一个排序的矢量。

 1#include<iostream>    
 2#include<vector> 
 3#include<algorithm>
 4using namespace std;
 5int main()
 6{  
 7    //vector initialisation
 8    vector<int> vec { 2,4,6,8,10 };
 9    
10    cout<<"Before sorting vector : ";
11    for(auto i=vec.begin(); i<vec.end(); i++)
12    {
13    	cout<<" "<<*i;
14    }
15    
16    std::sort(vec.begin(),vec.end(), greater<int>());//Sorting the vector using greater<int>() function
17    
18    cout<<"\n\nAfter sorting vector : ";
19    for(auto i=vec.begin(); i<vec.end(); i++)
20    {
21    	cout<<" "<<*i;
22    }
23    return 0;
24}

出发点:

1Before sorting vector :  2 4 6 8 10
2
3After sorting vector :  10 8 6 4 2

使用 Lambda 表达式在 sort() 中

由于 C++11,使用 lambda 表达式被引入到 C++ 编程中,它们只是简单的单行函数,不需要声明,甚至不需要指定返回类型。

因此,我们可以使用我们自己的定义的 lambda 表达式来确定按 sort() 函数进行排序的顺序. 通过将单行表达式定义为 sort() 函数的第三参数来完成。

 1#include<iostream>    
 2#include<vector> 
 3#include<algorithm>
 4using namespace std;
 5int main()
 6{  
 7    //vector initialisation
 8    vector<int> vec { 11,22,33,44,55 };
 9    
10    cout<<"Before sorting vector : ";
11    for(auto i=vec.begin(); i<vec.end(); i++)
12    {
13    	cout<<" "<<*i;
14    }
15    
16    std::sort(vec.begin(),vec.end(), [](int &a, int &b){ return a>b; });
17        //Sorting the vector using user-defined lambda expression(return type bool)
18    
19    cout<<"\n\nAfter sorting vector : ";
20    for(auto i=vec.begin(); i<vec.end(); i++)
21    {
22    	cout<<" "<<*i;
23    }
24    return 0;
25}

出发点:**

1Before sorting vector :  11 22 33 44 55
2
3After sorting vector :  55 44 33 22 11

在这里,用a>b表达式来比较从矢量中传递的两个参数,正如我们可以从上面的代码的输出中看到的那样,矢量按照所需的 下行顺序进行排序。

结论

因此,在本文中,我们了解了如何在C++中以上升和下降顺序分类矢量,对于任何与此主题相关的进一步问题,请使用下面的评论。

参考

Published At
Categories with 技术
Tagged with
comments powered by Disqus