copy 元素拷贝
函数签名:
template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);
函数功能:
将源容器中指定区间内的元素拷贝到目标容器。目标容器需要提前开辟出足够的空间。
参数说明:
- first 源容器开始迭代器(包含)
- last 源容器结束迭代器(不包含)
- result 目标目前开启迭代器
函数实现:
template<class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{
while (first!=last) {
*result = *first;
++result; ++first;
}
return result;
}
应用案例:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
// 源容器
vector<int>v = {1,2,3,4,5,6,7,8};
// 目标容器,提前开辟足够的空间
vector<int>v1(v.size());
copy(v.begin(),v.end(),v1.begin());
for (vector<int>::iterator p = v1.begin(); p != v1.end(); p++) {
cout << *p << " ";
}
cout << endl; // 1 2 3 4 5 6 7 8
return 0;
}
replace 等值替换
函数签名:
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,const T& old_value, const T& new_value);
函数功能:
将目标容器指定区间内的指定值得元素替换为新的值
参数说明:
- first 目标容器起始迭代器(包含)
- last 目标容器结束迭代器(不包含)
- old_value 原值
- new_value 新值
函数实现:
函数内部实现等价于:
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value)
{
while (first!=last) {
if (*first == old_value) *first=new_value;
++first;
}
}
应用案例:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class MyPrint {
public:
void operator()(const int& v) {
cout << v << " ";
}
};
int main() {
vector<int> v = {1,2,3,4,1,2,3,4};
// 将v中的4替换成14
replace(v.begin(), v.end(), 4, 14);
for_each(v.begin(), v.end(), MyPrint());
cout << endl; // 1 2 3 14 1 2 3 14
return 0;
}
replace_if 按条件替换
函数签名:
template <class ForwardIterator, class UnaryPredicate, class T>
void replace_if (ForwardIterator first, ForwardIterator last,UnaryPredicate pred, const T& new_value );
函数功能:
将目标容器指定区间内复合指定条件的元素值替换为新的值
参数说明:
- first 目标容器起始迭代器(包含)
- last 目标容器结束迭代器(不包含)
- pred 替换条件,可以是函数指针,也可以是函数对象(一元谓词)
- new_value 新值
函数实现:
template < class ForwardIterator, class UnaryPredicate, class T >
void replace_if (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred, const T& new_value)
{
while (first!=last) {
if (pred(*first)) *first=new_value;
++first;
}
}
应用案例:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class MyPrint {
public:
void operator()(const int& v) {
cout << v << " ";
}
};
class MyCondition {
public:
bool operator()(const int& v) {
return v % 2 == 0;
}
};
int main() {
vector<int> v = { 1,2,3,4,1,2,3,4 };
// 将v中的偶数替换成0
replace_if(v.begin(), v.end(), MyCondition(), 0);
for_each(v.begin(), v.end(), MyPrint());
cout << endl; // 1 0 3 0 1 0 3 0
return 0;
}
swap 元素互换
函数签名:
template <class T>
void swap (T& a, T& b);
函数签名:
交换两个容器中的元素(存储的元素类型相同)
参数说明:
- a 容器1
- b 容器2
函数实现:
函数内部实现等价于:
template <class T> void swap ( T& a, T& b )
{
T c(a); a=b; b=c;
}
应用案例:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class MyPrint {
public:
void operator()(const int& v) {
cout << v << " ";
}
};
int main() {
vector<int> v1 = { 1,2,3,4,1,2,3,4 };
vector<int> v2 = { 3,4,9,7 };
// 交换 v1 与 v2 中的元素
swap(v1,v2);
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl; // 3 4 9 7
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl; // 1 2 3 4 1 2 3 4
return 0;
}