填充

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

using namespace std;//fill智能填充固定的值

int main()
{
	list<string> s;

	s.push_back("AAAAA");
	s.push_back("SSVA");
	s.push_back("DSGDSA");
	for (auto iter = s.begin(); iter != s.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

	fill(s.begin(), s.end(), "hao");//没有n的用迭代器指定区间
	for (auto iter = s.begin(); iter != s.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

	list<string> s1;//fill_n可以指定填充的个数,因为里边是空数据就必须使用插入迭代器
	fill_n(back_inserter(s1), 9, "world");
	for (auto iter = s1.begin(); iter != s1.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

	//直接填充到cout输出流迭代器
	fill_n(ostream_iterator<float>(cout, " "),10, 7.7);
	fill(s.begin(), s.end(), "haha");
	cout << endl;
	for (auto iter = s.begin(); iter != s.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

	fill_n(s1.begin(),s1.size()-2, "dddddd");//填充一部分

	for (auto iter = s1.begin(); iter != s1.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

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

	fill(++pos1, --pos2, "lsms");
	for (auto iter = s1.begin(); iter != s1.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

	list<int> ilist;//如果没有指定容器的容量,就要用back——inserter
	generate_n(back_inserter(ilist), 5, rand);//插入5个随机数
	for (auto iter = ilist.begin(); iter != ilist.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

	generate(ilist.begin(), ilist.end(), rand);//插入5个随机数
	for (auto iter = ilist.begin(); iter != ilist.end(); ++iter)
		cout << *iter << " ";
	cout << endl;
	system("pause");
	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值