排序算法:
sort(),stable_sort(),partial_sort(),nth_element()。
sort
sort是STL中最常用的排序算法,它可以对容器中的元素进行排序。sort的时间复杂度为O(NlogN),使用快速排序算法实现。sort的基本使用方法如下:
sort(begin, end); // 对区间[begin, end)内的元素排序
其中,begin和end分别是容器中要排序的元素的起始和结束迭代器。
此外,sort还可以接受一个可调用对象作为参数,来实现自定义的排序方法。例如,可以按照元素的某个属性进行排序:
sort(v.begin(), v.end(), [](const auto& a, const auto& b) {
return a.property < b.property;
}); // 按照元素的property属性进行排序
stable_sort
stable_sort与sort的用法类似,也可以对容器中的元素进行排序,但它保证了相等元素的相对顺序不会发生改变。stable_sort的时间复杂度为O(NlogN),使用归并排序算法实现。stable_sort的基本使用方法如下:
stable_sort(begin, end); // 对区间[begin, end)内的元素排序
partial_sort
partial_sort可以对容器中的元素进行部分排序,即只将前k个最小(或最大)的元素放在容器的前k个位置上。partial_sort的时间复杂度为O(Nlogk),使用堆排序算法实现。partial_sort的基本使用方法如下:
partial_sort(begin, middle, end);
// 将区间[begin, end)内的前middle-begin个元素排序,其他元素保持原有顺序
其中,middle是一个迭代器,指向容器中排序后前middle-begin个元素的末尾位置。
nth_element
nth_element可以找出容器中第k小(或第k大)的元素。nth_element的时间复杂度为O(N),使用快速选择算法实现。nth_element的基本使用方法如下:
nth_element(begin, nth, end);
// 将区间[begin, end)内的元素排序,使得第nth个元素是第nth小的元素
其中,nth是一个迭代器,指向容器中第nth小的元素。
查找算法:
find(),find_if(),find_if_not(),binary_search(),lower_bound(),upper_bound(),equal_range()。
find()
在容器中查找指定元素并返回迭代器。如果找到了指定元素,则返回指向该元素的迭代器,否则返回指向容器末尾的迭代器。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec {1, 2, 3, 4, 5};
auto it = std::find(vec.begin(), vec.end(), 3);
if (it != vec.end()) {
std::cout << "Found: " << *it << '\n';
} else {
std::cout << "Not found\n";
}
return 0;
}
find_if()
在容器中查找符合指定条件的元素并返回迭代器。该算法接受一个谓词函数作为参数,用于判断容器中的元素是否符合条件。
#include <iostream>
#include <vector>
#include <algorithm>
bool is_odd(int n) {
return n % 2 != 0;
}
int main() {
std::vector<int> vec {1, 2, 3, 4, 5};
auto it = std::find_if(vec.begin(), vec.end(), is_odd);
if (it != vec.end()) {
std::cout << "Found: " << *it << '\n';
} else {
std::cout << "Not found\n";
}
return 0;
}
find_if_not()
与find_if算法类似,但是它查找不符合指定条件的元素。
#include <iostream>
#include <vector>
#include <algorithm>
bool is_odd(int n) {
return n % 2 != 0;
}
int main() {
std::vector<int> vec {1, 2, 3, 4, 5};
auto it = std::find_if_not(vec.begin(), vec.end(), is_odd);
if (it != vec.end()) {
std::cout << "Found: " << *it << '\n';
} else {
std::cout << "Not found\n";
}
return 0;
}
binary_search()
在已排序的容器中使用二分查找算法查找指定元素。如果找到了指定元素,则返回true,否则返回false。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec {1, 2, 3, 4, 5};
bool found = std::binary_search(vec.begin(), vec.end(), 3);
if (found) {
std::cout << "Found\n";
} else {
std::cout << "Not found\n";
}
return 0;
}
lower_bound()
在已排序的容器中查找第一个大于或等于指定元素的位置,并返回迭代器。如果找到了指定元素,则返回指向该元素的迭代器;否则返回指向第一个大于该元素的迭代器。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec {1, 2, 3, 3, 4, 5};
auto it = std::lower_bound(vec.begin(), vec.end(), 3);
if (it != vec.end()) {
std::cout << "Found: " << *it << '\n';
} else {
std::cout << "Not found\n";
}
return 0;
}
upper_bound()
与lower_bound算法类似,但是它查找第一个大于指定元素的位置。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec {1, 2, 3, 3, 4, 5};
auto it = std::upper_bound(vec.begin(), vec.end(), 3);
if (it != vec.end()) {
std::cout << "Found: " << *it << '\n';
} else {
std::cout << "Not found\n";
}
return 0;
}
equal_range()
在已排序的容器中查找指定元素的范围,并返回一对迭代器。这对迭代器包含了所有等于指定元素的元素的范围。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec {1, 2, 3, 3, 4, 5};
auto p = std::equal_range(vec.begin(), vec.end(), 3);
if (p.first != vec.end() && p.second != vec.end()) {
std::cout << "Found: ";
for (auto it = p.first; it != p.second; ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
} else {
std::cout << "Not found\n";
}
return 0;
}
数值算法:
accumulate(),adjacent_difference(),inner_product(),partial_sum()。
accumulate()
计算区间内元素的总和,可以指定初始值。
int a[] = {1, 2, 3, 4, 5};
int sum = accumulate(a, a + 5, 0);
adjacent_difference()
计算相邻元素之间的差值并存储到目标区间中。
int a[] = {1, 2, 3, 4, 5};
int b[5];
adjacent_difference(a, a + 5, b);
inner_product()
计算两个区间内元素的点积(即对应元素相乘后相加),可以指定初始值。
int a[] = {1, 2, 3, 4, 5};
int b[] = {2, 4, 6, 8, 10};
int result = inner_product(a, a + 5, b, 10);
partial_sum()
计算区间内元素的部分和并存储到目标区间中,可以指定初始值。
int a[] = {1, 2, 3, 4, 5};
int b[5];
partial_sum(a, a + 5, b);
移动算法:
move(),move_backward()
move()
将一个范围内的元素移动到另一个范围内。
vector<string> v1{"hello", "world"};
vector<string> v2(2);
std::move(v1.begin(), v1.end(), v2.begin());
// v1: {"", ""}, v2: {"hello", "world"}
std::move_backward()
将一个范围内的元素移动到另一个范围内,保持元素顺序不变。
vector<string> v1{"hello", "world"};
vector<string> v2(2);
std::move_backward(v1.begin(), v1.end(), v2.end());
// v1: {"", ""}, v2: {"hello", "world"}
堆算法:
make_heap(),push_heap(),pop_heap(),sort_heap()。
make_heap()
将一个序列变成一个堆。
int a[] = {3, 1, 4, 1, 5, 9};
make_heap(a, a + 6);
push_heap()
将一个元素插入到堆中,并保持堆的性质。
int a[] = {9, 5, 4, 1, 1};
push_heap(a, a + 5);
a[5] = 6;
push_heap(a, a + 6);
pop_heap()
将堆顶元素移到序列末尾,并保持堆的性质。
int a[] = {9, 6, 4, 1, 1, 5};
pop_heap(a, a + 6);
sort_heap()
将一个堆排序。
int a[] = {6, 1, 4, 1, 5, 9};
make_heap(a, a + 6);
sort_heap(a, a + 6);
迭代器算法:
copy(),reverse(),unique()。
copy()
将一个序列复制到另一个序列中。
int a[] = {1, 2, 3, 4, 5};
int b[5];
copy(a, a + 5, b);
reverse()
将一个序列翻转。
int a[] = {1, 2, 3, 4, 5};
reverse(a, a + 5);
unique()
去除一个序列中的重复元素。
int a[] = {1, 2, 2, 3, 3, 3, 4, 5, 5};
int* p = unique(a, a + 9);
607

被折叠的 条评论
为什么被折叠?



