copy |
将一个范围中的元素拷贝到新的位置处 |
copy_backward |
将一个范围中的元素按逆序拷贝到新的位置处 |
copy_if C++11 |
将一个范围中满足给定条件的元素拷贝到新的位置处 |
copy_n C++11 |
拷贝 n 个元素到新的位置处 |
fill |
将一个范围的元素赋值为给定值 |
fill_n |
将某个位置开始的 n 个元素赋值为给定值 |
generate |
将一个函数的执行结果保存到指定范围的元素中,用于批量赋值范围中的元素 |
generate_n |
将一个函数的执行结果保存到指定位置开始的 n 个元素中 |
iter_swap |
交换两个迭代器指向的元素 |
move C++11 |
将一个范围中的元素移动到新的位置处 |
move_backward C++11 |
将一个范围中的元素按逆序移动到新的位置处 |
random_shuffle |
随机打乱指定范围中的元素的位置 |
remove |
将一个范围中值等价于给定值的元素删除 |
remove_if |
将一个范围中值满足给定条件的元素删除 |
remove_copy |
拷贝一个范围的元素,将其中值等价于给定值的元素删除 |
remove_copy_if |
拷贝一个范围的元素,将其中值满足给定条件的元素删除 |
replace |
将一个范围中值等价于给定值的元素赋值为新的值 |
replace_copy |
拷贝一个范围的元素,将其中值等价于给定值的元素赋值为新的值 |
replace_copy_if |
拷贝一个范围的元素,将其中值满足给定条件的元素赋值为新的值 |
replace_if |
将一个范围中值满足给定条件的元素赋值为新的值 |
reverse |
反转排序指定范围中的元素 |
reverse_copy |
拷贝指定范围的反转排序结果 |
rotate |
循环移动指定范围中的元素 |
rotate_copy |
拷贝指定范围的循环移动结果 |
shuffle C++11 |
用指定的随机数引擎随机打乱指定范围中的元素的位置 |
swap |
交换两个对象的值 |
swap_ranges |
交换两个范围的元素 |
transform |
对指定范围中的每个元素调用某个函数以改变元素的值 |
unique |
删除指定范围中的所有连续重复元素,仅仅留下每组等值元素中的第一个元素 |
unique_copy |
拷贝指定范围的唯一化(参考上述的 unique)结果 |
将一个范围中的元素拷贝到新的位置处
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// copy algorithm example #include <iostream> // std::cout #include <algorithm> // std::copy #include <vector> // std::vector int main () { int myints[]={ 10, 20, 30, 40, 50, 60, 70}; std::vector< int> myvector ( 7); std::copy ( myints, myints+ 7, myvector.begin() ); std::cout << "myvector contains:"; for (std::vector< int>::iterator it = myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 10 20 30 40 50 60 70
|
将一个范围中的元素按逆序拷贝到新的位置处
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// copy_backward example #include <iostream> // std::cout #include <algorithm> // std::copy_backward #include <vector> // std::vector int main () { std::vector< int> myvector; // set some values: for ( int i= 1; i<= 5; i++) myvector.push_back(i* 10); // myvector: 10 20 30 40 50 myvector.resize(myvector.size()+ 3); // allocate space for 3 more elements std::copy_backward ( myvector.begin(), myvector.begin()+ 5, myvector.end() ); std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 10 20 30 10 20 30 40 50
|
将一个范围中满足给定条件的元素拷贝到新的位置处
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// copy_if example #include <iostream> // std::cout #include <algorithm> // std::copy_if, std::distance #include <vector> // std::vector int main () { std::vector< int> foo = { 25, 15, 5,- 5,- 15}; std::vector< int> bar (foo.size()); // copy only positive numbers: auto it = std::copy_if (foo.begin(), foo.end(), bar.begin(), []( int i){ return !(i< 0);} ); bar.resize(std::distance(bar.begin(),it)); // shrink container to new size std::cout << "bar contains:"; for ( int& x: bar) std::cout << ' ' << x; std::cout << '\n'; return 0; } |
输出:
1
|
bar contains: 25 15 5
|
拷贝 n 个元素到新的位置处
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// copy_n algorithm example #include <iostream> // std::cout #include <algorithm> // std::copy #include <vector> // std::vector int main () { int myints[]={ 10, 20, 30, 40, 50, 60, 70}; std::vector< int> myvector; myvector.resize( 7); // allocate space for 7 elements std::copy_n ( myints, 7, myvector.begin() ); std::cout << "myvector contains:"; for (std::vector< int>::iterator it = myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 10 20 30 40 50 60 70
|
将一个范围的元素赋值为给定值
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// fill algorithm example #include <iostream> // std::cout #include <algorithm> // std::fill #include <vector> // std::vector int main () { 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'; return 0; } |
输出:
1
|
myvector contains: 5 5 5 8 8 8 0 0
|
将某个位置开始的 n 个元素赋值为给定值
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// fill_n example #include <iostream> // std::cout #include <algorithm> // std::fill_n #include <vector> // std::vector int main () { std::vector< int> myvector ( 8, 10); // myvector: 10 10 10 10 10 10 10 10 std::fill_n (myvector.begin(), 4, 20); // myvector: 20 20 20 20 10 10 10 10 std::fill_n (myvector.begin()+ 3, 3, 33); // myvector: 20 20 20 33 33 33 10 10 std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 20 20 20 33 33 33 10 10
|
将一个函数的执行结果保存到指定范围的元素中,用于批量赋值范围中的元素
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// generate algorithm example #include <iostream> // std::cout #include <algorithm> // std::generate #include <vector> // std::vector #include <ctime> // std::time #include <cstdlib> // std::rand, std::srand // 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 main () { std::srand ( unsigned ( std::time( 0) ) ); std::vector< int> myvector ( 8); generate (myvector.begin(), myvector.end(), RandomNumber); std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; std::generate (myvector.begin(), myvector.end(), UniqueNumber); std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
2 |
myvector contains: 57 87 76 66 85 54 17 15
myvector contains: 1 2 3 4 5 6 7 8 |
将一个函数的执行结果保存到指定位置开始的 n 个元素中
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// generate_n example #include <iostream> // std::cout #include <algorithm> // std::generate_n int current = 0; int UniqueNumber () { return ++current; } int main () { int myarray[ 9]; std::generate_n (myarray, 9, UniqueNumber); std::cout << "myarray contains:"; for ( int i= 0; i< 9; ++i) std::cout << ' ' << myarray[i]; std::cout << '\n'; return 0; } |
输出:
1
|
myarray contains: 1 2 3 4 5 6 7 8 9
|
交换两个迭代器指向的元素
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// iter_swap example #include <iostream> // std::cout #include <algorithm> // std::iter_swap #include <vector> // std::vector int main () { int myints[]={ 10, 20, 30, 40, 50 }; // myints: 10 20 30 40 50 std::vector< int> myvector ( 4, 99); // myvector: 99 99 99 99 std::iter_swap(myints,myvector.begin()); // myints: [99] 20 30 40 50 // myvector: [10] 99 99 99 std::iter_swap(myints+ 3,myvector.begin()+ 2); // myints: 99 20 30 [99] // myvector: 10 99 [40] 99 std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 10 99 40 99
|
将一个范围中的元素移动到新的位置处
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// move algorithm example #include <iostream> // std::cout #include <algorithm> // std::move (ranges) #include <utility> // std::move (objects) #include <vector> // std::vector #include <string> // std::string int main () { std::vector<std::string> foo = { "air", "water", "fire", "earth"}; std::vector<std::string> bar ( 4); // moving ranges: std::cout << "Moving ranges...\n"; std::move ( foo.begin(), foo.begin()+ 4, bar.begin() ); std::cout << "foo contains " << foo.size() << " elements:"; std::cout << " (each in an unspecified but valid state)"; std::cout << '\n'; std::cout << "bar contains " << bar.size() << " elements:"; for (std::string& x: bar) std::cout << " [" << x << "]"; std::cout << '\n'; // moving container: std::cout << "Moving container...\n"; foo = std::move (bar); std::cout << "foo contains " << foo.size() << " elements:"; for (std::string& x: foo) std::cout << " [" << x << "]"; std::cout << '\n'; std::cout << "bar is in an unspecified but valid state"; std::cout << '\n'; return 0; } |
输出:
1
2 3 4 5 6 |
Moving ranges...
foo contains 4 elements: (each in an unspecified but valid state) bar contains 4 elements: [air] [water] [fire] [earth] Moving container... foo contains 4 elements: [air] [water] [fire] [earth] bar is in an unspecified but valid state |
将一个范围中的元素按逆序移动到新的位置处
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// move_backward example #include <iostream> // std::cout #include <algorithm> // std::move_backward #include <string> // std::string int main () { std::string elems[ 10] = { "air", "water", "fire", "earth"}; // insert new element at the beginning: std::move_backward (elems,elems+ 4,elems+ 5); elems[ 0]= "ether"; std::cout << "elems contains:"; for ( int i= 0; i< 10; ++i) std::cout << " [" << elems[i] << "]"; std::cout << '\n'; return 0; } |
输出:
1
|
elems contains: [ether] [air] [water] [fire] [earth] [] [] [] [] []
|
随机打乱指定范围中的元素的位置
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// random_shuffle example #include <iostream> // std::cout #include <algorithm> // std::random_shuffle #include <vector> // std::vector #include <ctime> // std::time #include <cstdlib> // std::rand, std::srand // random generator function: int myrandom ( int i) { return std::rand()%i;} int main () { std::srand ( unsigned ( std::time( 0) ) ); std::vector< int> myvector; // set some values: for ( int i= 1; i< 10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 // using built-in random generator: std::random_shuffle ( myvector.begin(), myvector.end() ); // using myrandom: std::random_shuffle ( myvector.begin(), myvector.end(), myrandom); // print out content: std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 3 4 1 6 8 9 2 7 5
|
将一个范围中值等价于给定值的元素删除
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// remove algorithm example #include <iostream> // std::cout #include <algorithm> // std::remove int main () { 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'; return 0; } |
输出:
1
|
range contains: 10 30 30 10 10
|
将一个范围中值满足给定条件的元素删除
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// remove_if example #include <iostream> // std::cout #include <algorithm> // std::remove_if bool IsOdd ( int i) { return ((i% 2)== 1); } int main () { int myints[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9}; // 1 2 3 4 5 6 7 8 9 // bounds of range: int* pbegin = myints; // ^ int* pend = myints+ sizeof(myints)/ sizeof( int); // ^ ^ pend = std::remove_if (pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ? // ^ ^ std::cout << "the range contains:"; for ( int* p=pbegin; p!=pend; ++p) std::cout << ' ' << *p; std::cout << '\n'; return 0; } |
输出:
1
|
the range contains: 2 4 6 8
|
拷贝一个范围的元素,将其中值等价于给定值的元素删除
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// remove_copy example #include <iostream> // std::cout #include <algorithm> // std::remove_copy #include <vector> // std::vector int main () { int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20}; // 10 20 30 30 20 10 10 20 std::vector< int> myvector ( 8); std::remove_copy (myints,myints+ 8,myvector.begin(), 20); // 10 30 30 10 10 0 0 0 std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 10 30 30 10 10 0 0 0
|
拷贝一个范围的元素,将其中值满足给定条件的元素删除
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// remove_copy_if example #include <iostream> // std::cout #include <algorithm> // std::remove_copy_if #include <vector> // std::vector bool IsOdd ( int i) { return ((i% 2)== 1); } int main () { int myints[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::vector< int> myvector ( 9); std::remove_copy_if (myints,myints+ 9,myvector.begin(),IsOdd); std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 2 4 6 8 0 0 0 0 0
|
将一个范围中值等价于给定值的元素赋值为新的值
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// replace algorithm example #include <iostream> // std::cout #include <algorithm> // std::replace #include <vector> // std::vector int main () { int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; std::vector< int> myvector (myints, myints+ 8); // 10 20 30 30 20 10 10 20 std::replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 99 std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 10 99 30 30 99 10 10 99
|
拷贝一个范围的元素,将其中值等价于给定值的元素赋值为新的值
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// replace_copy example #include <iostream> // std::cout #include <algorithm> // std::replace_copy #include <vector> // std::vector int main () { int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; std::vector< int> myvector ( 8); std::replace_copy (myints, myints+ 8, myvector.begin(), 20, 99); std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 10 99 30 30 99 10 10 99
|
拷贝一个范围的元素,将其中值满足给定条件的元素删除
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// replace_copy_if example #include <iostream> // std::cout #include <algorithm> // std::replace_copy_if #include <vector> // std::vector bool IsOdd ( int i) { return ((i% 2)== 1); } int main () { std::vector< int> foo,bar; // set some values: for ( int i= 1; i< 10; i++) foo.push_back(i); // 1 2 3 4 5 6 7 8 9 bar.resize(foo.size()); // allocate space std::replace_copy_if (foo.begin(), foo.end(), bar.begin(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0 std::cout << "bar contains:"; for (std::vector< int>::iterator it=bar.begin(); it!=bar.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
second contains: 0 2 0 4 0 6 0 8 0
|
将一个范围中值满足给定条件的元素赋值为新的值
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// replace_if example #include <iostream> // std::cout #include <algorithm> // std::replace_if #include <vector> // std::vector bool IsOdd ( int i) { return ((i% 2)== 1); } int main () { std::vector< int> myvector; // set some values: for ( int i= 1; i< 10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::replace_if (myvector.begin(), myvector.end(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0 std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 0 2 0 4 0 6 0 8 0
|
反转排序指定范围中的元素
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// reverse algorithm example #include <iostream> // std::cout #include <algorithm> // std::reverse #include <vector> // std::vector int main () { std::vector< int> myvector; // set some values: for ( int i= 1; i< 10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2 1 // print out content: std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 9 8 7 6 5 4 3 2 1
|
拷贝指定范围的反转排序结果
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// reverse_copy example #include <iostream> // std::cout #include <algorithm> // std::reverse_copy #include <vector> // std::vector int main () { int myints[] ={ 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::vector< int> myvector; myvector.resize( 9); // allocate space std::reverse_copy (myints, myints+ 9, myvector.begin()); // print out content: std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 9 8 7 6 5 4 3 2 1
|
循环移动指定范围中的元素
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// rotate algorithm example #include <iostream> // std::cout #include <algorithm> // std::rotate #include <vector> // std::vector int main () { std::vector< int> myvector; // set some values: for ( int i= 1; i< 10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::rotate(myvector.begin(),myvector.begin()+ 3,myvector.end()); // 4 5 6 7 8 9 1 2 3 // print out content: std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 4 5 6 7 8 9 1 2 3
|
拷贝指定范围的循环移动结果
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// rotate_copy algorithm example #include <iostream> // std::cout #include <algorithm> // std::rotate_copy #include <vector> // std::vector int main () { int myints[] = { 10, 20, 30, 40, 50, 60, 70}; std::vector< int> myvector ( 7); std::rotate_copy(myints,myints+ 3,myints+ 7,myvector.begin()); // print out content: std::cout << "myvector contains:"; for (std::vector< int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 40 50 60 70 10 20 30
|
用指定的随机数引擎随机打乱指定范围中的元素的位置
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// shuffle algorithm example #include <iostream> // std::cout #include <algorithm> // std::move_backward #include <array> // std::array #include <random> // std::default_random_engine #include <chrono> // std::chrono::system_clock int main () { std::array< int, 5> foo { 1, 2, 3, 4, 5}; // obtain a time-based seed: unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); shuffle (foo.begin(), foo.end(), std::default_random_engine(seed)); std::cout << "shuffled elements:"; for ( int& x: foo) std::cout << ' ' << x; std::cout << '\n'; return 0; } |
输出:
1
|
shuffled elements: 3 1 4 2 5
|
交换两个对象的值
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// swap algorithm example (C++98) #include <iostream> // std::cout #include <algorithm> // std::swap #include <vector> // std::vector int main () { int x= 10, y= 20; // x:10 y:20 std::swap(x,y); // x:20 y:10 std::vector< int> foo ( 4,x), bar ( 6,y); // foo:4x20 bar:6x10 std::swap(foo,bar); // foo:6x10 bar:4x20 std::cout << "foo contains:"; for (std::vector< int>::iterator it=foo.begin(); it!=foo.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
foo contains: 10 10 10 10 10 10
|
交换两个范围的元素
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// swap_ranges example #include <iostream> // std::cout #include <algorithm> // std::swap_ranges #include <vector> // std::vector int main () { std::vector< int> foo ( 5, 10); // foo: 10 10 10 10 10 std::vector< int> bar ( 5, 33); // bar: 33 33 33 33 33 std::swap_ranges(foo.begin()+ 1, foo.end()- 1, bar.begin()); // print out results of swap: std::cout << "foo contains:"; for (std::vector< int>::iterator it=foo.begin(); it!=foo.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; std::cout << "bar contains:"; for (std::vector< int>::iterator it=bar.begin(); it!=bar.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
2 |
foo contains: 10 33 33 33 10
bar contains: 10 10 10 33 33 |
对指定范围中的每个元素调用某个函数以改变元素的值
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// transform algorithm example #include <iostream> // std::cout #include <algorithm> // std::transform #include <vector> // std::vector #include <functional> // std::plus int op_increase ( int i) { return ++i; } int main () { std::vector< int> foo; std::vector< int> bar; // set some values: for ( int i= 1; i< 6; i++) foo.push_back (i* 10); // foo: 10 20 30 40 50 bar.resize(foo.size()); // allocate space std::transform (foo.begin(), foo.end(), bar.begin(), op_increase); // bar: 11 21 31 41 51 // std::plus adds together its two arguments: std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus< int>()); // foo: 21 41 61 81 101 std::cout << "foo contains:"; for (std::vector< int>::iterator it=foo.begin(); it!=foo.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
foo contains: 21 41 61 81 101
|
删除指定范围中的所有连续重复元素,仅仅留下每组等值元素中的第一个元素
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// unique algorithm example #include <iostream> // std::cout #include <algorithm> // std::unique, std::distance #include <vector> // std::vector bool myfunction ( int i, int j) { return (i==j); } int main () { int myints[] = { 10, 20, 20, 20, 30, 30, 20, 20, 10}; // 10 20 20 20 30 30 20 20 10 std::vector< int> myvector (myints,myints+ 9); // using default comparison: std::vector< int>::iterator it; it = std::unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ? // ^ myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 20 10 // using predicate comparison: std::unique (myvector.begin(), myvector.end(), myfunction); // (no changes) // print out content: std::cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 10 20 30 20 10
|
拷贝指定范围的唯一化(参考上述的 unique)结果
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// unique_copy example #include <iostream> // std::cout #include <algorithm> // std::unique_copy, std::sort, std::distance #include <vector> // std::vector bool myfunction ( int i, int j) { return (i==j); } int main () { int myints[] = { 10, 20, 20, 20, 30, 30, 20, 20, 10}; std::vector< int> myvector ( 9); // 0 0 0 0 0 0 0 0 0 // using default comparison: std::vector< int>::iterator it; it=std::unique_copy (myints,myints+ 9,myvector.begin()); // 10 20 30 20 10 0 0 0 0 // ^ std::sort (myvector.begin(),it); // 10 10 20 20 30 0 0 0 0 // ^ // using predicate comparison: it=std::unique_copy (myvector.begin(), it, myvector.begin(), myfunction); // 10 20 30 20 30 0 0 0 0 // ^ myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 // print out content: std::cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
输出:
1
|
myvector contains: 10 20 30
|
特别说明:函数的中文释义来自:http://classfoo.cn/cpp/head/76573_319/,例子来自:http://www.cplusplus.com/reference/algorithm/