1つ、コピー
機能の説明:
- コンテナ内の指定された範囲の要素を別のコンテナにコピーします
関数プロトタイプ:
-
copy(iterator beg, iterator end, iterator dest);
//値で要素を検索し、指定された位置に戻るイテレータを検索し、最後に戻るイテレータを検索します
// begがイテレータを開始します
//終了はイテレータを終了します
// destターゲット開始イテレータ
例:
#include <algorithm>
#include <vector>
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i + 1);
}
vector<int> v2;
v2.resize(v1.size());
copy(v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), v2.end(), myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
概要:コピーアルゴリズムを使用してコピーする場合、ターゲットコンテナは事前にスペースを開くことを忘れないでください
二、交換
機能の説明:
- コンテナの指定された範囲内の古い要素を新しい要素に変更します
関数プロトタイプ:
-
replace(iterator beg, iterator end, oldvalue, newvalue);
//間隔内の古い要素を新しい要素に置き換えます
// begがイテレータを開始します
//終了はイテレータを終了します
// oldvalueold要素
// newvalue new element
例:
#include <algorithm>
#include <vector>
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int> v;
v.push_back(20);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(50);
v.push_back(10);
v.push_back(20);
cout << "替换前:" << endl;
for_each(v.begin(), v.end(), myPrint());
cout << endl;
//将容器中的20 替换成 2000
cout << "替换后:" << endl;
replace(v.begin(), v.end(), 20,2000);
for_each(v.begin(), v.end(), myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
概要:replaceは、間隔内の条件を満たす要素を置き換えます
三、replace_if
機能の説明:
- 条件を満たす間隔の要素を指定された要素に置き換えます
関数プロトタイプ:
-
replace_if(iterator beg, iterator end, _pred, newvalue);
//条件に従って要素を置き換え、条件を満たす要素を指定された要素に置き換えます
// begがイテレータを開始します
//終了はイテレータを終了します
// _ pred述語
//新しい要素がnewvalueに置き換えられました
例:
#include <algorithm>
#include <vector>
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
class ReplaceGreater30
{
public:
bool operator()(int val)
{
return val >= 30;
}
};
void test01()
{
vector<int> v;
v.push_back(20);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(50);
v.push_back(10);
v.push_back(20);
cout << "替换前:" << endl;
for_each(v.begin(), v.end(), myPrint());
cout << endl;
//将容器中大于等于的30 替换成 3000
cout << "替换后:" << endl;
replace_if(v.begin(), v.end(), ReplaceGreater30(), 3000);
for_each(v.begin(), v.end(), myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
概要:replace_ifは条件で検索されます。ファンクターを使用して、満たされている条件を柔軟にフィルタリングできます
4、スワップ
機能の説明:
- 2つのコンテナの要素を交換します
関数プロトタイプ:
-
swap(container c1, container c2);
// 2つのコンテナの要素を交換します
// c1コンテナ1
// c2コンテナ2
例:
#include <algorithm>
#include <vector>
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i+100);
}
cout << "交换前: " << endl;
for_each(v1.begin(), v1.end(), myPrint());
cout << endl;
for_each(v2.begin(), v2.end(), myPrint());
cout << endl;
cout << "交换后: " << endl;
swap(v1, v2);
for_each(v1.begin(), v1.end(), myPrint());
cout << endl;
for_each(v2.begin(), v2.end(), myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
概要:コンテナを交換するときは、同じタイプのコンテナに注意してください。