set和vector互相转换

set和vector互相转换

用起始迭代器和结束迭代器实现

#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
void print01(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	set<int> s1(v1.begin(), v1.end());
	vector<int> v2;
	v2.assign(s1.begin(), s1.end());
	cout << "打印set里的数据" << endl;
	for_each(s1.begin(), s1.end(), print01);
	cout << endl;
	cout << "打印vec里的数据" << endl;
	for_each(v2.begin(), v2.end(), print01);
}
int main() {
	test01();
	return 0;
}

  

 

 

posted @ 2022-06-06 23:58  今天也是开心的一天呀  阅读(1648)  评论(0)    收藏  举报