C++中元組向量的排序(升序)
本文將討論如何按升序對C++中的元組向量進行排序。元組是C++中一種用於儲存元素列表的資料結構。它可以包含相同或不同的資料型別,並且我們可以按照初始化時作為輸入的順序訪問它們。元組的資料按順序組織,以便我們可以按相同的順序檢索它。
語法
tuple<data type-1, data type-2, data type-3,….> name
在C++中,我們可以這樣初始化一個元組。我們可能需要更多元組函式來對元組向量進行排序。
make_tuple()
此函式用於建立元組。我們可以使用此函式根據初始化元組時傳遞的引數將值儲存到元組中。
語法
tuple<int, int, string> t; t=make_tuple(5, 4, “hello”);
make_tuple()函式會將傳遞給它的值儲存到已初始化的元組中。
get<>()
此函式用於獲取特定元組值或訪問元組的值。
語法
tuple<int, int, int, int> t=make_tuple(1, 2, 3, 4); cout<<get<2>(t);
輸出
3
注意:與向量一樣,元組也遵循從0開始的索引來訪問其中儲存的元素。
向量是C++資料結構的一種形式,也可以用於儲存相同資料型別元素的列表。它們類似於動態陣列,允許在執行時更改大小。
可以使用以下語法初始化任何資料型別的向量:
#include <vector> vector<data type> name;
我們可以傳遞任何資料型別,例如int、string等,來初始化特定資料型別的向量。
我們將討論如何排列元組向量,使其按升序排序。元組向量可以透過根據元組中包含的第一個元素、儲存在元組中的第二個元素或其他方式對向量進行排序來排序。
在對元組向量進行排序之前,讓我們先學習如何在C++中初始化元組向量。
示例
//C++ code to show vector of tuples is created and how to print it
#include <bits/stdc++.h>
using namespace std;
int main()
{
//initialise a vector of tuples by passing tuple through it
//initialise a tuple to store 4 integers
vector<tuple<int, int, int, int>> vec;
//store tuples in vector using push_back() function
//tuples are initialised using make_tuple() function
vec.push_back(make_tuple(2,2,4,9));
vec.push_back(make_tuple(5,3,1,8));
vec.push_back(make_tuple(7,3,-3,10));
vec.push_back(make_tuple(8,12,32,6));
int s=vec.size(); //to get the size of the vector
for(int i=0;i<s;i++){ //to print the elements stored in vector of tuples, vec
cout<<get<0>(vec[i])<<" "<<get<1>(vec[i])<<" "
<<get<2>(vec[i])<<" "<<get<3>(vec[i])<<endl;
}
return 0;
}
輸出
2 2 4 9 5 3 1 8 7 3 -3 10 8 12 32 6
方法1(根據第一個元素)
我們可以使用C++中提供的內建庫sort()對元組向量進行排序。
預設情況下,此函式按升序對資料結構中的元素進行排序。
函式的語法為:
vector<int> v={5,8,7,3,1};
sort(v.begin(),v.end());
使用sort()函式時,通常給出兩個引數。第一個引數指定我們必須從中排序元素的位置,第二個引數指定我們必須對其排序元素的位置。我們的標準允許我們也傳遞第三個元件。例如,假設我們希望按降序對向量或陣列進行排序。
sort()函式預設情況下按第一個元素的升序排序。sort()函式可以簡單地根據元組中包含的第一個元素對元組向量進行排序。
注意:如果在根據第一個元素對元組向量排序時元組的第一個元素相等,則它將預設情況下根據其後續元素對這些元組進行排序。
上述方法的C++程式碼:
示例
//function to sort vector of tuples with respect to first elements
#include <bits/stdc++.h>
using namespace std;
int main()
{
//initialise a vector of tuples by passing tuple through it
//initialise a tuple to store 4 integers
vector<tuple<int, int, int, int>> vec;
//store tuples in vector using push_back() function
//tuples are initialised using make_tuple() function
vec.push_back(make_tuple(10,2,14,9));
vec.push_back(make_tuple(7,3,-3,10));
vec.push_back(make_tuple(5,2,11,8));
vec.push_back(make_tuple(8,12,5,6));
//sort the vector of tuples in ascending order with respect to the first element
sort(vec.begin(),vec.end());
int s=vec.size(); //to get the size of the vector
for(int i=0;i<s;i++){ //to print the elements stored in vector of tuples, vec
cout<<get<0>(vec[i])<<" "<<get<1>(vec[i])<<" "
<<get<2>(vec[i])<<" "<<get<3>(vec[i])<<endl;
}
return 0;
}
輸出
5 2 11 8 7 3 -3 10 8 12 5 6 10 2 14 9
時間複雜度:O(N logN),其中N是向量的長度。
空間複雜度:O(1),因為我們沒有使用任何額外的空間。
方法2(根據第三個元素)
在這種情況下,我們將根據元組中存在的第三個元素對元組向量進行排序。為了根據元組中的第三個元素進行排序,我們將像上述方法一樣使用sort()函式,但會對其進行修改。
眾所周知,我們可以根據需要在sort()函式中傳遞第三個引數,以便按照我們想要的方式對元素進行排序。
為了根據第三個元素對向量進行排序,我們將傳遞一個布林函式作為第三個引數,只是為了檢查元組的第三個元素是否小於下一個元組的第三個元素,以便按升序排序。
根據元組中的第三個元素進行排序的C++程式碼:
示例
//C++ code to sort the vector of tuples with respect to third element
#include <bits/stdc++.h>
using namespace std;
//function to compare third elements present in the tuple
bool third(tuple<int, int, int, int>& p,
tuple<int, int, int, int>& q){
return get<2>(p)<get<2>(q);
}
int main()
{
//initialise a vector of tuples by passing tuple through it
//initialise a tuple to store 4 integers
vector<tuple<int, int, int, int>> vec;
//store tuples in vector using push_back() function
//tuples are initialised using make_tuple() function
vec.push_back(make_tuple(10,2,14,9));
vec.push_back(make_tuple(7,3,-3,10));
vec.push_back(make_tuple(5,2,11,8));
vec.push_back(make_tuple(8,12,5,6));
//sort the vector of tuples in ascending order with respect to the first element
sort(vec.begin(),vec.end(),third);
int s=vec.size(); //to get the size of the vector
for(int i=0;i<s;i++){ //to print the elements stored in vector of tuples, vec
cout<<get<0>(vec[i])<<" "<<get<1>(vec[i])<<" "
<<get<2>(vec[i])<<" "<<get<3>(vec[i])<<endl;
}
return 0;
}
輸出
7 3 -3 10 8 12 5 6 5 2 11 8 10 2 14 9
時間複雜度:O(N logN),其中N是向量的長度。
空間複雜度:O(1),因為我們沒有使用任何額外的空間。
類似地,我們可以透過在一個布林函式中比較元組中指定的元素,根據元組中存在的任何元素對元組向量進行排序。
結論
我們討論瞭如何在C++中根據元組中存在的任何元素按升序對任何元組向量進行排序。我們討論瞭如何使用sort()函式對元組向量進行排序,以及如何透過在sort()函式中傳遞第三個引數來根據元組中元素的任何位置對元組向量進行排序。
我希望在閱讀本文後,您關於該主題的所有疑問都已得到解答。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP