码迷,mamicode.com
首页 > 编程语言 > 详细

STL_算法(21)_ STL_算法_填充新值

时间:2016-08-21 22:56:11      阅读:462      评论:0      收藏:0      [点我收藏+]

标签:

STL_算法_填充新值

fill(b, e, v)

fill(b, n, v)

generate(b, n, p)

generate_n(b, n, p)

技术分享

#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
//
#include<string>

using namespace std;

int main()
{
	list<string> slist;
	slist.push_back("hello");
	slist.push_back("hi");
	slist.push_back("good mornig");

	// fill   fill_n 算法只能填充固定值 
	fill(slist.begin(), slist.end(), "hao");

	for (list<string>::iterator iter = slist.begin(); iter != slist.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	list<string> slist2;
	fill_n(back_inserter(slist2), 9, "hello");
	for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	fill_n(ostream_iterator<float>(cout, " "), 10, 7.7);
	cout << endl;

	fill(slist2.begin(), slist2.end(), "again");

	for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	fill_n(slist2.begin(), slist2.size() - 2, "hi");
	for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	list<string>::iterator pos1, pos2;
	pos1 = slist2.begin(); 
	pos2 = slist2.end();

	fill(++pos1, --pos2, "hmmm");
	for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); iter++)
		cout << *iter << ' ';
	cout << endl;
	//
	system("pause");
	return 0;
}
技术分享


#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
//
#include<string>

using namespace std;

int main()
{
	list<string> slist;
	slist.push_back("hello");
	slist.push_back("hi");
	slist.push_back("good mornig");

	// fill   fill_n 算法只能填充固定值 
	fill(slist.begin(), slist.end(), "hao");

	for (list<string>::iterator iter = slist.begin(); iter != slist.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	list<string> slist2;
	fill_n(back_inserter(slist2), 9, "hello");
	for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	fill_n(ostream_iterator<float>(cout, " "), 10, 7.7);
	cout << endl;

	fill(slist2.begin(), slist2.end(), "again");

	for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	fill_n(slist2.begin(), slist2.size() - 2, "hi");
	for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	list<string>::iterator pos1, pos2;
	pos1 = slist2.begin(); 
	pos2 = slist2.end();

	fill(++pos1, --pos2, "hmmm");
	for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	list<int> ilist;

	// back_inserter()  插入迭代器
	generate_n(back_inserter(ilist), 5, rand);
	for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	generate(ilist.begin(), ilist.end(), rand);
	for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout << *iter << ' ';
	cout << endl;



	//
	system("pause");
	return 0;
}

技术分享



STL_算法(21)_ STL_算法_填充新值

标签:

原文地址:http://blog.csdn.net/taotaoah/article/details/52269724

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!