#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;
}
填充
最新推荐文章于 2024-01-06 05:30:00 发布