cb41a_c++_STL_算法_填充新值fill_generate

cb41a_c++_STL_算法_填充新值fill_generate
fill(b,e,v)
fill_n(b,n,v),填充n个v
generate(b,e,p)
generate_n(b,n,p)

generate_n(back_inserter(ilist), 5, rand);//插入5个随机数,rand是个随机数函数


STL算法-修改性算法
for_each()
copy()
copy_backward()
transform()
merge()
swap_ranges()
fill()
fill_n()

generate()
generate_n()
replace()
replace_if()
replace_copy()
replace_copy_if()

  1 /*cb41a_c++_STL_算法_填充新值fill_generate
  2 fill(b,e,v)
  3 fill_n(b,n,v),填充n个v
  4 generate(b,e,p)
  5 generate_n(b,n,p)
  6 
  7 generate_n(back_inserter(ilist), 5, rand);//插入5个随机数,rand是个随机数函数
  8 
  9 
 10 STL算法-修改性算法
 11 for_each()
 12 copy()
 13 copy_backward()
 14 transform()
 15 merge()
 16 swap_ranges()
 17 fill()
 18 fill_n()
 19 
 20 generate()
 21 generate_n()
 22 replace()
 23 replace_if()
 24 replace_copy()
 25 replace_copy_if()
 26 */
 27 
 28 #include <iostream>
 29 #include <algorithm>
 30 #include <vector>
 31 #include <list>
 32 #include <string>
 33 #include <iterator>
 34 
 35 
 36 using namespace std;
 37 
 38 int main()
 39 {
 40     list<string> slist;
 41     slist.push_back("hello");
 42     //slist.push_back("hi");
 43     slist.push_back("helaaalo");
 44     slist.push_back("morning");
 45     fill(slist.begin(), slist.end(), "hao");
 46 
 47     for (list<string>::iterator iter = slist.begin(); iter != slist.end(); ++iter)
 48         cout << *iter << ' ';
 49     cout << endl;
 50 
 51     list<string> slist2;
 52     cout << "slist2里面没有数据,就需要back_inserter来插入,否则报错" << endl;
 53     fill_n(back_inserter(slist2), 9, "hello");
 54     for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); ++iter)
 55         cout << *iter << ' ';
 56     cout << endl;
 57 
 58     cout << "直接填充到cout输出流里面" << endl;
 59     fill_n(ostream_iterator<float>(cout, " "), 10, 7.7);
 60     cout << endl;
 61 
 62     fill(slist2.begin(), slist2.end(), "again");
 63     for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); ++iter)
 64         cout << *iter << ' ';
 65     cout << endl;
 66 
 67     fill_n(slist2.begin(), slist2.size() - 2, "hi");//填充(slist2.size() - 2)个hi
 68     
 69     for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); ++iter)
 70         cout << *iter << ' ';
 71     cout << endl;
 72 
 73     list<string>::iterator pos1, pos2;
 74     pos1 = slist2.begin();
 75     pos2 = slist2.end();
 76 
 77     cout << "除了前后一个。中间填充hmmm" << endl;
 78 
 79     fill(++pos1, --pos2, "hmmm");
 80 
 81     for (list<string>::iterator iter = slist2.begin(); iter != slist2.end(); ++iter)
 82         cout << *iter << ' ';
 83     cout << endl;
 84 
 85     cout << "使用generate填充5个随机数" << endl;
 86 
 87     list<int> ilist;
 88     generate_n(back_inserter(ilist), 5, rand);//插入5个随机数
 89     for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); ++iter)
 90         cout << *iter << ' ';
 91     cout << endl;
 92 
 93     cout << "使用generate填充随机数" << endl;
 94     generate(ilist.begin(), ilist.end(), rand);
 95     for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); ++iter)
 96         cout << *iter << ' ';
 97     cout << endl;
 98 
 99 
100     return 0;
101 }

 

posted @ 2020-02-23 18:38  txwtech  阅读(194)  评论(0编辑  收藏  举报