อาร์เรย์และเวกเตอร์เป็นโครงสร้างข้อมูลที่สำคัญมากในการเขียนโปรแกรมเชิงแข่งขันสำหรับการแก้ปัญหา และ STL (ไลบรารีเทมเพลตมาตรฐาน ) ในการเขียนโปรแกรม c++ มีฟังก์ชันบางอย่างเพื่อดำเนินการกับอาร์เรย์และเวกเตอร์
มาดูการทำงานของฟังก์ชันเหล่านี้กัน
การหาผลรวม ต่ำสุด และสูงสุดของอาร์เรย์/เวกเตอร์ - ใน 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