c++ accumulate、find、count、fill、fill_n、copy、sort、unique 泛型算法
c++ accumulate、find、count、fill、fill_n、copy、sort、unique 泛型算法
- `accumulate`
- `find`
- `count`
- `fill`
- `fill_n`
- `copy`
- `sort`
- `unique`
- 注意事项
在C++中,STL(标准模板库)提供了许多泛型算法,用于处理容器中的元素。以下是你提到的几种算法的函数声明及其简单使用示例。
accumulate
- 函数声明:
template< class InputIt, class T > T accumulate( InputIt first, InputIt last, T init );
- 功能:计算区间
[first, last)
内元素的累加和,初始值为init
。 - 示例:
#include <iostream> #include <vector> #include <numeric> // accumulate int main() { std::vector<int> v = {1, 2, 3, 4, 5}; int sum = std::accumulate(v.begin(), v.end(), 0); std::cout << "Sum: " << sum << std::endl; // 输出: Sum: 15 return 0; }
find
- 函数声明:
template< class InputIt, class T > InputIt find( InputIt first, InputIt last, const T& value );
- 功能:在区间
[first, last)
内查找值为value
的元素,返回指向该元素的迭代器。如果未找到,返回last
。 - 示例:
#include <iostream> #include <vector> #include <algorithm> // find int main() { std::vector<int> v = {1, 2, 3, 4, 5}; auto it = std::find(v.begin(), v.end(), 3); if (it != v.end()) { std::cout << "Found: " << *it << std::endl; // 输出: Found: 3 } else { std::cout << "Not found" << std::endl; } return 0; }
count
- 函数声明:
template< class InputIt, class T > typename iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T& value );
- 功能:计算区间
[first, last)
内等于value
的元素的个数。 - 示例:
#include <iostream> #include <vector> #include <algorithm> // count int main() { std::vector<int> v = {1, 2, 3, 3, 4, 3}; int cnt = std::count(v.begin(), v.end(), 3); std::cout << "Count: " << cnt << std::endl; // 输出: Count: 3 return 0; }
fill
- 函数声明:
template< class ForwardIt, class T > void fill( ForwardIt first, ForwardIt last, const T& value );
- 功能:将区间
[first, last)
内的所有元素赋值为value
。 - 示例:
#include <iostream> #include <vector> #include <algorithm> // fill int main() { std::vector<int> v(5); // 创建一个包含5个元素的vector std::fill(v.begin(), v.end(), 10); for (int i : v) { std::cout << i << " "; // 输出: 10 10 10 10 10 } return 0; }
fill_n
- 函数声明:
template< class OutputIt, class Size, class T > OutputIt fill_n( OutputIt first, Size count, const T& value );
- 功能:从
first
开始,将count
个元素赋值为value
。 - 示例:
#include <iostream> #include <vector> #include <algorithm> // fill_n int main() { std::vector<int> v(5); // 创建一个包含5个元素的vector std::fill_n(v.begin(), 3, 10); // 将前3个元素赋值为10 for (int i : v) { std::cout << i << " "; // 输出: 10 10 10 0 0 } return 0; }
copy
- 函数声明:
template< class InputIt, class OutputIt > OutputIt copy( InputIt first, InputIt last, OutputIt d_first );
- 功能:将区间
[first, last)
内的元素复制到以d_first
开始的目标区间。 - 示例:
#include <iostream> #include <vector> #include <algorithm> // copy int main() { std::vector<int> src = {1, 2, 3, 4, 5}; std::vector<int> dst(5); std::copy(src.begin(), src.end(), dst.begin()); for (int i : dst) { std::cout << i << " "; // 输出: 1 2 3 4 5 } return 0; }
sort
- 函数声明:
template< class RandomIt > void sort( RandomIt first, RandomIt last );
- 功能:对区间
[first, last)
内的元素进行升序排序。 - 示例:
#include <iostream> #include <vector> #include <algorithm> // sort int main() { std::vector<int> v = {5, 3, 1, 4, 2}; std::sort(v.begin(), v.end()); for (int i : v) { std::cout << i << " "; // 输出: 1 2 3 4 5 } return 0; }
unique
- 函数声明:
template< class ForwardIt > ForwardIt unique( ForwardIt first, ForwardIt last );
- 功能:移除区间
[first, last)
内相邻的重复元素,返回指向新逻辑结尾的迭代器。 - 示例:
#include <iostream> #include <vector> #include <algorithm> // unique int main() { std::vector<int> v = {1, 2, 2, 3, 3, 3, 4}; auto last = std::unique(v.begin(), v.end()); v.erase(last, v.end()); // 删除多余的元素 for (int i : v) { std::cout << i << " "; // 输出: 1 2 3 4 } return 0; }
注意事项
- 在使用写算法(如
fill
,copy
等)时,必须确保目标区间足够大,否则会导致未定义行为。 sort
和unique
通常结合使用,unique
只能移除相邻的重复元素,因此在使用unique
之前通常需要先对容器进行排序。