您的位置:首页 > 新闻 > 会展 > STL——常用算法(二)

STL——常用算法(二)

2025/1/9 23:03:30 来源:https://blog.csdn.net/qq_74811378/article/details/139971463  浏览:    关键词:STL——常用算法(二)

一、常用拷贝和替换算法

1.copy

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}vector<int>v2;v2.resize(v1.size());copy(v1.begin(), v1.end(), v2.begin());for_each(v2.begin(), v2.end(), printVector);cout << endl;
}
int main()
{test01();return 0;
}

2.replace

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}replace(v1.begin(), v1.end(), 5, 10);for_each(v1.begin(), v1.end(), printVector);cout << endl;
}
int main()
{test01();return 0;
}

3.replace_if

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
class Greater5
{
public:bool operator()(int val){return val >= 5;}
};
void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}for_each(v1.begin(), v1.end(), MyPrint());cout << endl;replace_if(v1.begin(), v1.end(), Greater5(), 10);for_each(v1.begin(), v1.end(), MyPrint());cout << endl;
}
int main()
{test01();return 0;
}

4.swap

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
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);}for_each(v1.begin(), v1.end(), MyPrint());cout << endl;for_each(v2.begin(), v2.end(), MyPrint());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();return 0;
}

二、常用算数生成算法

1.accumulate

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i <= 100; i++){v1.push_back(i);}int total = accumulate(v1.begin(), v1.end(), 1000);//5050+1000//for_each(v1.begin(), v1.end(), MyPrint());cout << total << endl;
}
int main()
{test01();return 0;
}

2.fill

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{vector<int>v1;v1.resize(10);for_each(v1.begin(), v1.end(), MyPrint());cout << endl;fill(v1.begin(), v1.end(), 10);for_each(v1.begin(), v1.end(), MyPrint());
}
int main()
{test01();return 0;
}

三、常用集合算法

1.set_intersection

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(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 + 5);}vector<int>v3;v3.resize(min(v1.size(), v2.size()));vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), itEnd, MyPrint);cout << endl;
}
int main()
{test01();return 0;
}

2.set_union

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(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 + 5);}vector<int>v3;v3.resize(v1.size() + v2.size());vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), itEnd, MyPrint);cout << endl;
}
int main()
{test01();return 0;
}

3.set_difference

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(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 + 5);}vector<int>v3;v3.resize(max(v1.size(), v2.size()));vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), itEnd, MyPrint);cout << endl;vector<int>::iterator itEnd1 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());for_each(v3.begin(), itEnd1, MyPrint);cout << endl;
}
int main()
{test01();return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com