algorithm此头文件是算法库的一部分。本篇介绍修改序列的操作函数。(2)
修改序列的操作 | |
fill | 将一个给定值复制赋值给一个范围内的每个元素 (函数模板) |
fill_n | 将一个给定值复制赋值给一个范围内的 N 个元素 (函数模板) |
generate | 将相继的函数调用结果赋值给一个范围中的每个元素 (函数模板) |
generate_n | 将相继的函数调用结果赋值给一个范围中的 N 个元素 (函数模板) |
removeremove_if | 移除满足特定判别标准的元素 (函数模板) |
remove_copyremove_copy_if | 复制一个范围的元素,忽略满足特定判别标准的元素 (函数模板) |
reverse | 逆转范围中的元素顺序 (函数模板) |
reverse_copy | 创建一个范围的逆向副本 (函数模板) |
rotate | 旋转范围中的元素顺序 (函数模板) |
rotate_copy | 复制并旋转元素范围 (函数模板) |
random_shuffleshuffle (C++17 前)(C++11) | 随机重排范围中的元素 (函数模板) |
sample (C++17) | 从一个序列中随机选择 N 个元素 (函数模板) |
unique | 移除范围内的连续重复元素 (函数模板) |
unique_copy | 创建某范围的不含连续重复元素的副本 (函数模板) |
示例代码:
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <utility> // std::pair
#include <vector>
#include <array>
#include <cctype> // std::tolower
#include <string>
#include <ctime> // std::time
#include <cstdlib> // std::rand, std::srand
#include <random> // std::default_random_engine
#include <chrono> // std::chrono::system_clock
// function generator:
int RandomNumber() { return (std::rand() % 100); }
// class generator:
struct c_unique {
int current;
c_unique() { current = 0; }
int operator()() { return ++current; }
} UniqueNumber;
int current = 0;
int UniqueNumber2() { return ++current; }
bool IsOdd4(int i) { return ((i % 2) == 1); }
bool myfunction9(int i, int j) {
return (i == j);
}
int myrandom(int i) { return std::rand() % i; }
int main()
{
// fill algorithm example
std::vector<int> myvector(8); // myvector: 0 0 0 0 0 0 0 0
std::fill(myvector.begin(), myvector.begin() + 4, 5); // myvector: 5 5 5 5 0 0 0 0
std::fill(myvector.begin() + 3, myvector.end() - 2, 8); // myvector: 5 5 5 8 8 8 0 0
std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
// fill_n example
std::vector<int> myvector2(8, 10); // myvector2: 10 10 10 10 10 10 10 10
std::fill_n(myvector2.begin(), 4, 20); // myvector2: 20 20 20 20 10 10 10 10
std::fill_n(myvector2.begin() + 3, 3, 33); // myvector2: 20 20 20 33 33 33 10 10
std::cout << "myvector2 contains:";
for (std::vector<int>::iterator it = myvector2.begin(); it != myvector2.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
// generate algorithm example
std::srand(unsigned(std::time(0)));
std::vector<int> myvector3(8);
std::generate(myvector3.begin(), myvector3.end(), RandomNumber);
std::cout << "myvector3 contains:";
for (std::vector<int>::iterator it = myvector3.begin(); it != myvector3.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::generate(myvector3.begin(), myvector3.end(), UniqueNumber);
std::cout << "myvector3 contains:";
for (std::vector<int>::iterator it = myvector3.begin(); it != myvector3.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
//generate_n example
int myarray[9];
std::generate_n(myarray, 9, UniqueNumber2);
std::cout << "myarray contains:";
for (int i = 0; i < 9; ++i)
std::cout << ' ' << myarray[i];
std::cout << '\n';
//remove example
int myints[] = { 10,20,30,30,20,10,10,20 }; // 10 20 30 30 20 10 10 20
// bounds of range:
int* pbegin = myints; // ^
int* pend = myints + sizeof(myints) / sizeof(int); // ^ ^
pend = std::remove(pbegin, pend, 20); // 10 30 30 10 10 ? ? ?
// ^ ^
std::cout << "range contains:";
for (int* p = pbegin; p != pend; ++p)
std::cout << ' ' << *p;
std::cout << '\n';
//remove_if example
int myints5[] = { 1,2,3,4,5,6,7,8,9 }; // 1 2 3 4 5 6 7 8 9
// bounds of range:
int* pbegin5 = myints5; // ^
int* pend5 = myints5 + sizeof(myints5) / sizeof(int); // ^ ^
pend5 = std::remove_if(pbegin5, pend5, IsOdd4); // 2 4 6 8 ? ? ? ? ?
// ^ ^
std::cout << "the range contains:";
for (int* p = pbegin5; p != pend5; ++p)
std::cout << ' ' << *p;
std::cout << '\n';
//remove_copy example
int myints6[] = { 10,20,30,30,20,10,10,20 }; // 10 20 30 30 20 10 10 20
std::vector<int> myvector6(8);
std::remove_copy(myints6, myints6 + 8, myvector6.begin(), 20); // 10 30 30 10 10 0 0 0
std::cout << "myvector6 contains:";
for (std::vector<int>::iterator it = myvector6.begin(); it != myvector6.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
//remove_copy_if example
int myints7[] = { 1,2,3,4,5,6,7,8,9 };
std::vector<int> myvector7(9);
std::remove_copy_if(myints7, myints7 + 9, myvector7.begin(), IsOdd4);
std::cout << "myvector7 contains:";
for (std::vector<int>::iterator it = myvector7.begin(); it != myvector7.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
//unique example
int myints8[] = { 10,20,20,20,30,30,20,20,10 }; // 10 20 20 20 30 30 20 20 10
std::vector<int> myvector8(myints8, myints8 + 9);
// using default comparison:
std::vector<int>::iterator it8;
it8 = std::unique(myvector8.begin(), myvector8.end()); // 10 20 30 20 10 ? ? ? ?
// ^
myvector8.resize(std::distance(myvector8.begin(), it8)); // 10 20 30 20 10
// using predicate comparison:
std::unique(myvector8.begin(), myvector8.end(), myfunction9); // (no changes)
// print out content:
std::cout << "myvector8 contains:";
for (it8 = myvector8.begin(); it8 != myvector8.end(); ++it8)
std::cout << ' ' << *it8;
std::cout << '\n';
//unique_copy example
int myints9[] = { 10,20,20,20,30,30,20,20,10 };
std::vector<int> myvector9(9); // 0 0 0 0 0 0 0 0 0
// using default comparison:
std::vector<int>::iterator it9;
it9 = std::unique_copy(myints9, myints9 + 9, myvector9.begin()); // 10 20 30 20 10 0 0 0 0
// ^
std::sort(myvector9.begin(), it9); // 10 10 20 20 30 0 0 0 0
// ^
// using predicate comparison:
it9 = std::unique_copy(myvector9.begin(), it9, myvector9.begin(), myfunction9);
// 10 20 30 20 30 0 0 0 0
// ^
myvector9.resize(std::distance(myvector9.begin(), it9)); // 10 20 30
// print out content:
std::cout << "myvector9 contains:";
for (it9 = myvector9.begin(); it9 != myvector9.end(); ++it9)
std::cout << ' ' << *it9;
std::cout << '\n';
//reverse example
std::vector<int> myvector10;
// set some values:
for (int i = 1; i < 10; ++i) myvector10.push_back(i); // 1 2 3 4 5 6 7 8 9
std::reverse(myvector10.begin(), myvector10.end()); // 9 8 7 6 5 4 3 2 1
// print out content:
std::cout << "myvector10 contains:";
for (std::vector<int>::iterator it = myvector10.begin(); it != myvector10.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
//reverse_copy example
int myints11[] = { 1,2,3,4,5,6,7,8,9 };
std::vector<int> myvector11;
myvector11.resize(9); // allocate space
std::reverse_copy(myints11, myints11 + 9, myvector11.begin());
// print out content:
std::cout << "myvector11 contains:";
for (std::vector<int>::iterator it = myvector11.begin(); it != myvector11.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
//rotate example
std::vector<int> myvector12;
// set some values:
for (int i = 1; i < 10; ++i) myvector12.push_back(i); // 1 2 3 4 5 6 7 8 9
std::rotate(myvector12.begin(), myvector12.begin() + 3, myvector12.end());
// 4 5 6 7 8 9 1 2 3
// print out content:
std::cout << "myvector12 contains:";
for (std::vector<int>::iterator it = myvector12.begin(); it != myvector12.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
//rotate_copy example
int myints13[] = { 10,20,30,40,50,60,70 };
std::vector<int> myvector13(7);
std::rotate_copy(myints13, myints13 + 3, myints13 + 7, myvector13.begin());
// print out content:
std::cout << "myvector13 contains:";
for (std::vector<int>::iterator it = myvector13.begin(); it != myvector13.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
//random_shuffle example
std::srand(unsigned(std::time(0)));
std::vector<int> myvector14;
// set some values:
for (int i = 1; i < 10; ++i) myvector14.push_back(i); // 1 2 3 4 5 6 7 8 9
// using built-in random generator:
std::random_shuffle(myvector14.begin(), myvector14.end());
// using myrandom:
std::random_shuffle(myvector14.begin(), myvector14.end(), myrandom);
// print out content:
std::cout << "myvector14 contains:";
for (std::vector<int>::iterator it = myvector14.begin(); it != myvector14.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
//shuffle
std::array<int, 5> foo15{ 1,2,3,4,5 };
// obtain a time-based seed:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
shuffle(foo15.begin(), foo15.end(), std::default_random_engine(seed));
std::cout << "shuffled elements:";
for (int& x : foo15) std::cout << ' ' << x;
std::cout << '\n';
std::cout << "hello world\n";
return 0;
}
运行结果:
参考:
https://cplusplus.com/reference/algorithm/
标准库标头 <algorithm> - cppreference.com