使用C++ STL運算元組和向量


陣列和向量是競賽程式設計中非常重要的資料結構,用於解決問題。C++程式設計中的STL(標準模板庫)提供了一些函式來執行陣列和向量的操作。

讓我們看看其中一些函式的實際應用:

查詢陣列/向量的和、最小值和最大值 - STL 中有一些函式可以幫助你查詢陣列/向量的和、最大值和最小值。函式及其功能如下:

查詢和

accumulate(startIndex, endIndex, initialSum)

陣列/向量的最大元素

*max_element(startIndex, endIndex)

陣列/向量的最小元素

*min_element(startIndex, endIndex)

對陣列執行操作的程式:

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   int array[] = {65, 7,12, 90, 31, 113 };
   int l = sizeof(array) / sizeof(array[0]);
   cout<<"The elments of the array are : ";
   for(int i = 0; i<l; i++)
   cout<<array[i]<<"\t";
   cout<<endl;
   cout<<"The sum of all elements of the array: "<<accumulate(array, array + l, 0)<<endl;
   cout<<"The element with maximum value in array: "<<*max_element(array, array + l)<<endl;
   cout<<"The element with minimum value in array: "<<*min_element(array, array + l)<<endl;
   return 0;
}

輸出

The elments of the array are : 65 7 12 90 31 113
The sum of all elements of the array: 318
The element with maximum value in array: 113
The element with minimum value in array: 7

對向量執行操作的程式:

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   vector<int> vec= {65, 7,12, 90, 31, 113 };
   cout<<"The sum of all elments of the vector: "<<accumulate(vec.begin(), vec.end() + 1, 0)<<endl;
   cout<<"The element with maximum value in vector: "<<*max_element(vec.begin(), vec.end())<<endl;
   cout<<"The element with minimum value in vector: "<<*min_element(vec.begin(), vec.end())<<endl;
   return 0;
}

輸出

The sum of all elments of the vector: 318
The element with maximum value in vector: 113
The element with minimum value in vector: 7

對陣列/向量的元素進行排序:

在STL中,有一個函式`sort()`可以用來對陣列/向量的元素進行排序。該函式使用快速排序技術對陣列/向量進行排序。

語法

sort(startIndex, endIndex)

對陣列元素進行排序的程式:

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   int array[] = {65, 7,12, 90, 31, 113 };
   int l = sizeof(array) / sizeof(array[0]);
   cout<<"The elments of the array are : ";
   for(int i = 0; i<l; i++)
      cout<<array[i]<<"\t";
   cout<<endl;
   cout<<"\nSorting elements of the array…\n\n";
   sort(array, array+l);
   cout<<"The sorted array is : ";
   for(int i = 0; i<l; i++)
      cout<<array[i]<<"\t";
   cout<<endl;
   return 0;
}

輸出

The elments of the array are : 65 7 12 90 31 113
Sorting elements of the array...
The sorted array is : 7 12 31 65 90 113

對向量元素進行排序的程式:

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   vector<int> vec = {65, 7,12, 90, 31, 113 };
   cout<<"The elments of the vector are : ";
   for(int i = 0; i<vec.size(); i++)
      cout<<vec[i]<<"\t";
   cout<<endl;
   cout<<"\nSorting elements of the vector...\n\n";
   sort(vec.begin(), vec.end());
   cout<<"The sorted vector is : ";
   for(int i = 0; i<vec.size(); i++)
      cout<<vec[i]<<"\t";
   cout<<endl;
   return 0;
}

輸出

The elments of the vector are : 65 7 12 90 31 113
Sorting elements of the vector...
The sorted vector is : 7 12 31 65 90 113

更新於:2020年7月17日

瀏覽量:331

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.