algorithm此头文件是算法库的一部分。本篇介绍修改序列的操作函数。
修改序列的操作 | |
copycopy_if (C++11) | 将某一范围的元素复制到一个新的位置 (函数模板) |
copy_n (C++11) | 将一定数目的元素复制到一个新的位置 (函数模板) |
copy_backward | 按从后往前的顺序复制一个范围内的元素 (函数模板) |
move (C++11) | 将某一范围的元素移动到一个新的位置 (函数模板) |
move_backward (C++11) | 按从后往前的顺序移动某一范围的元素到新的位置 (函数模板) |
transform | 将一个函数应用于某一范围的各个元素,并在目标范围存储结果 (函数模板) |
replacereplace_if | 将所有满足特定判别标准的值替换为另一个值 (函数模板) |
replace_copyreplace_copy_if | 复制一个范围,并将满足特定判别标准的元素替换为另一个值 (函数模板) |
swap | 交换两个对象的值 (函数模板) |
swap_ranges | 交换两个范围的元素 (函数模板) |
iter_swap | 交换两个迭代器所指向的元素 (函数模板) |
示例代码
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <utility> // std::pair
#include <vector>
#include <array>
#include <cctype> // std::tolower
#include <string>
int op_increase(int i) { return ++i; }
bool IsOdd12(int i) { return ((i % 2) == 1); }
int main()
{
// copy algorithm example
int myints1[] = { 10,20,30,40,50,60,70 };
std::vector<int> myvector1(7);
std::copy(myints1, myints1 + 7, myvector1.begin());
std::cout << "myvector1 contains:";
for (std::vector<int>::iterator it1 = myvector1.begin(); it1 != myvector1.end(); ++it1)
std::cout << ' ' << *it1;
std::cout << "\n";
// copy_n algorithm example
int myints2[] = { 10,20,30,40,50,60,70 };
std::vector<int> myvector2;
myvector2.resize(7); // allocate space for 7 elements
std::copy_n(myints2, 7, myvector2.begin());
std::cout << "myvector2 contains:";
for (std::vector<int>::iterator it2 = myvector2.begin(); it2 != myvector2.end(); ++it2)
std::cout << ' ' << *it2;
std::cout << "\n";
// copy_if example
std::vector<int> foo3 = { 25,15,5,-5,-15 };
std::vector<int> bar3(foo3.size());
// copy only positive numbers:
auto it3 = std::copy_if(foo3.begin(), foo3.end(), bar3.begin(), [](int i) {return !(i < 0); });
bar3.resize(std::distance(bar3.begin(), it3)); // shrink container to new size
std::cout << "bar3 contains:";
for (int& x : bar3)
std::cout << ' ' << x;
std::cout << '\n';
// copy_backward example
std::vector<int> myvector4;
// set some values:
for (int i = 1; i <= 5; i++)
myvector4.push_back(i * 10); // myvector4: 10 20 30 40 50
myvector4.resize(myvector4.size() + 3); // allocate space for 3 more elements
std::copy_backward(myvector4.begin(), myvector4.begin() + 5, myvector4.end());
std::cout << "myvector4 contains:";
for (std::vector<int>::iterator it4 = myvector4.begin(); it4 != myvector4.end(); ++it4)
std::cout << ' ' << *it4;
std::cout << '\n';
// move algorithm example
std::vector<std::string> foo5 = { "air","water","fire","earth" };
std::vector<std::string> bar5(4);
// moving ranges:
std::cout << "Moving ranges...\n";
std::move(foo5.begin(), foo5.begin() + 4, bar5.begin());
std::cout << "foo5 contains " << foo5.size() << " elements:";
std::cout << " (each in an unspecified but valid state)";
std::cout << '\n';
std::cout << "bar5 contains " << bar5.size() << " elements:";
for (std::string& x : bar5) std::cout << " [" << x << "]";
std::cout << '\n';
// moving container:
std::cout << "Moving container...\n";
foo5 = std::move(bar5);
std::cout << "foo5 contains " << foo5.size() << " elements:";
for (std::string& x : foo5) std::cout << " [" << x << "]";
std::cout << '\n';
std::cout << "bar5 is in an unspecified but valid state";
std::cout << '\n';
// move_backward example
std::string elems6[10] = { "air","water","fire","earth" };
// insert new element at the beginning:
std::move_backward(elems6, elems6 + 4, elems6 + 5);
elems6[0] = "ether";
std::cout << "elems6 contains:";
for (int i = 0; i < 10; ++i)
std::cout << " [" << elems6[i] << "]";
std::cout << '\n';
// swap algorithm example (C++98)
int x7 = 10, y7 = 20; // x7:10 y7:20
std::swap(x7, y7); // x7:20 y7:10
std::vector<int> foo7(4, x7), bar7(6, y7); // foo7:4x20 bar7:6x10
std::swap(foo7, bar7); // foo7:6x10 bar7:4x20
std::cout << "foo7 contains:";
for (std::vector<int>::iterator it7 = foo7.begin(); it7 != foo7.end(); ++it7)
std::cout << ' ' << *it7;
std::cout << '\n';
// swap_ranges example
std::vector<int> foo8(5, 10); // foo8: 10 10 10 10 10
std::vector<int> bar8(5, 33); // bar8: 33 33 33 33 33
std::swap_ranges(foo8.begin() + 1, foo8.end() - 1, bar8.begin());
// print out results of swap:
std::cout << "foo8 contains:";
for (std::vector<int>::iterator it8 = foo8.begin(); it8 != foo8.end(); ++it8)
std::cout << ' ' << *it8;
std::cout << '\n';
std::cout << "bar8 contains:";
for (std::vector<int>::iterator it8 = bar8.begin(); it8 != bar8.end(); ++it8)
std::cout << ' ' << *it8;
std::cout << '\n';
// iter_swap example
int myints9[] = { 10,20,30,40,50 }; // myints9: 10 20 30 40 50
std::vector<int> myvector9(4, 99); // myvector9: 99 99 99 99
std::iter_swap(myints9, myvector9.begin()); // myints9: [99] 20 30 40 50
// myvector9: [10] 99 99 99
std::iter_swap(myints9 + 3, myvector9.begin() + 2); // myints9: 99 20 30 [99] 50
// myvector9: 10 99 [40] 99
std::cout << "myvector9 contains:";
for (std::vector<int>::iterator it9 = myvector9.begin(); it9 != myvector9.end(); ++it9)
std::cout << ' ' << *it9;
std::cout << '\n';
// transform algorithm example
std::vector<int> foo10;
std::vector<int> bar10;
// set some values:
for (int i = 1; i < 6; i++)
foo10.push_back(i * 10); // foo10: 10 20 30 40 50
bar10.resize(foo10.size()); // allocate space
std::transform(foo10.begin(), foo10.end(), bar10.begin(), op_increase);
// bar10: 11 21 31 41 51
// std::plus adds together its two arguments:
std::transform(foo10.begin(), foo10.end(), bar10.begin(), foo10.begin(), std::plus<int>());
// foo: 21 41 61 81 101
std::cout << "foo10 contains:";
for (std::vector<int>::iterator it10 = foo10.begin(); it10 != foo10.end(); ++it10)
std::cout << ' ' << *it10;
std::cout << '\n';
// replace algorithm example
int myints11[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
std::vector<int> myvector11(myints11, myints11 + 8); // 10 20 30 30 20 10 10 20
std::replace(myvector11.begin(), myvector11.end(), 20, 99); // 10 99 30 30 99 10 10 99
std::cout << "myvector11 contains:";
for (std::vector<int>::iterator it11 = myvector11.begin(); it11 != myvector11.end(); ++it11)
std::cout << ' ' << *it11;
std::cout << '\n';
// replace_if 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::replace_if(myvector12.begin(), myvector12.end(), IsOdd12, 0); // 0 2 0 4 0 6 0 8 0
std::cout << "myvector12 contains:";
for (std::vector<int>::iterator it12 = myvector12.begin(); it12 != myvector12.end(); ++it12)
std::cout << ' ' << *it12;
std::cout << '\n';
// replace_copy example
int myints13[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
std::vector<int> myvector13(8);
std::replace_copy(myints13, myints13 + 8, myvector13.begin(), 20, 99);
std::cout << "myvector13 contains:";
for (std::vector<int>::iterator it13 = myvector13.begin(); it13 != myvector13.end(); ++it13)
std::cout << ' ' << *it13;
std::cout << '\n';
// replace_copy_if example
std::vector<int> foo14, bar14;
// set some values:
for (int i = 1; i < 10; i++)
foo14.push_back(i); // 1 2 3 4 5 6 7 8 9
bar14.resize(foo14.size()); // allocate space
std::replace_copy_if(foo14.begin(), foo14.end(), bar14.begin(), IsOdd12, 0);
// 0 2 0 4 0 6 0 8 0
std::cout << "bar14 contains:";
for (std::vector<int>::iterator it14 = bar14.begin(); it14 != bar14.end(); ++it14)
std::cout << ' ' << *it14;
std::cout << '\n';
std::cout << "hello world\n";
return 0;
}
运行结果:
参考:
https://cplusplus.com/reference/algorithm/
标准库标头 <algorithm> - cppreference.com