使用STL库,很容易对数组执行某些基本操作,例如排序,搜索,元素总和,查找数组的最小和最大元素。
排序
可以在sort()函数的帮助下进行排序。
sort(starting_index,last_index) –对给定的数组/向量进行排序。 sort()函数适用于快速排序算法。 C++ STL提供了类似的函数排序,可以对向量或数组(具有随机访问权限的项目)进行排序。它的复杂度为O(nlogn)。
例子:
Input: {1, 7, 2, 4, 8, 3}
Output: {1, 2, 3, 4, 7, 8}
Array
// C++ program to sort Array
// using sort() in STL
#include
using namespace std;
int main()
{
int a[] = { 1, 7, 2, 4, 8, 3 };
int l = sizeof(a) / sizeof(a[0]);
sort(a, a + l);
for (int i; i < l; i++)
cout << a[i] << " ";
return 0;
}
Vector
// C++ program to sort Vector
// using sort() in STL
#include
using namespace std;
int main()
{
vector a = { 1, 7, 2, 4, 8, 3 };
sort(a.begin(), a.end());
for (int i; i < a.size(); i++)
cout << a[i] << " ";
return 0;
}
Array
// C++ program to reverse Array
// using reverse() in STL
#include
using namespace std;
int main()
{
int a[] = { 1, 7, 2, 4, 8, 3 };
int l = sizeof(a) / sizeof(a[0]);
reverse(a, a + l);
for (int i = 0; i < l; i++)
cout << a[i] << " ";
return 0;
}
Vector
// C++ program to reverse Vector
// using reverse() in STL
#include
using namespace std;
int main()
{
vector a = { 1, 7, 2, 4, 8, 3 };
reverse(a.begin(), a.end());
for (int i; i < a.size(); i++)
cout << a[i] << " ";
return 0;
}
Array
// C++ program to find sum, max and min
// element of Array using STL
#include
using namespace std;
int main()
{
int a[] = { 1, 7, 2, 4, 8, 3 };
int l = sizeof(a) / sizeof(a[0]);
cout << "\nsum of array: "
<< accumulate(a, a + l, 0);
cout << "\nMaximum element in array: "
<< *max_element(a, a + l);
cout << "\nMinimum element in array: "
<< *min_element(a, a + l);
return 0;
}
Vector
// C++ program to find sum, max and min
// element of Vector using STL
#include
using namespace std;
int main()
{
vector a = { 1, 7, 2, 4, 8, 3 };
cout << "\nsum of vector: "
<< accumulate(a.begin(), a.end(), 0);
cout << "\nMaximum element in vector: "
<< *max_element(a.begin(), a.end());
cout << "\nMinimum element in vector: "
<< *min_element(a.begin(), a.end());
return 0;
}
输出:
1 2 3 4 7 8
撤销
可以通过reverse()函数来完成反向操作。
reverse(start_index,last_index) :反转给定的数组/向量。
例子:
Input: {1, 7, 2, 4, 8, 3}
Output: {3, 8, 4, 2, 7, 1}
大批
// C++ program to reverse Array
// using reverse() in STL
#include
using namespace std;
int main()
{
int a[] = { 1, 7, 2, 4, 8, 3 };
int l = sizeof(a) / sizeof(a[0]);
reverse(a, a + l);
for (int i = 0; i < l; i++)
cout << a[i] << " ";
return 0;
}
向量
// C++ program to reverse Vector
// using reverse() in STL
#include
using namespace std;
int main()
{
vector a = { 1, 7, 2, 4, 8, 3 };
reverse(a.begin(), a.end());
for (int i; i < a.size(); i++)
cout << a[i] << " ";
return 0;
}
输出:
3 8 4 2 7 1
查找总和,最大和最小元素
STL中完成上述工作的功能是:
- accumulate(first_index,last_index,sum的初始值):此函数返回数组/向量的所有元素的和。
- * max_element(first_index,last_index):查找数组/向量的最大元素。
- * min_element(first_index,last_index):查找数组/向量的最小元素。
大批
// C++ program to find sum, max and min
// element of Array using STL
#include
using namespace std;
int main()
{
int a[] = { 1, 7, 2, 4, 8, 3 };
int l = sizeof(a) / sizeof(a[0]);
cout << "\nsum of array: "
<< accumulate(a, a + l, 0);
cout << "\nMaximum element in array: "
<< *max_element(a, a + l);
cout << "\nMinimum element in array: "
<< *min_element(a, a + l);
return 0;
}
向量
// C++ program to find sum, max and min
// element of Vector using STL
#include
using namespace std;
int main()
{
vector a = { 1, 7, 2, 4, 8, 3 };
cout << "\nsum of vector: "
<< accumulate(a.begin(), a.end(), 0);
cout << "\nMaximum element in vector: "
<< *max_element(a.begin(), a.end());
cout << "\nMinimum element in vector: "
<< *min_element(a.begin(), a.end());
return 0;
}
输出:
sum of array: 25
Maximum element in array: 8
Minimum element in array: 1
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。