fill對區(qū)間填充
原型:
template < class ForwardIterator, class T > void fill ( ForwardIterator first , ForwardIterator last , const T& value ) { while (first != last) *first++ = value; }
填充區(qū)間[first,last )
示例:
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main () { vector < int > myvector ( 8 ); // myvector: 0 0 0 0 0 0 0 0 fill (myvector.begin(),myvector.begin() + 4 , 5 ); // myvector: 5 5 5 5 0 0 0 0 fill (myvector.begin()+ 3 ,myvector.end()- 2 , 8 ); // myvector: 5 5 5 8 8 8 0 0 cout << " myvector contains: " ; for (vector< int >::iterator it=myvector.begin(); it!=myvector.end(); ++ it) cout << " " << * it; cout << endl; return 0 ; }
fill_n可 指定填充的個數(shù)
原型:
template < class OutputIterator, class Size, class T > void fill_n ( OutputIterator first , Size n , const T& value ) { for (; n> 0 ; --n) *first++ = value; }
填充區(qū)間[first,first+n)
n為要填充的元素個數(shù)
示例:
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main () { vector < int > myvector ( 8 , 10 ); // myvector: 10 10 10 10 10 10 10 10 fill_n (myvector.begin(), 4 , 20 ); // myvector: 20 20 20 20 10 10 10 10 fill_n (myvector.begin()+ 3 , 3 , 33 ); // myvector: 20 20 20 33 33 33 10 10 cout << " myvector contains: " ; for (vector< int >::iterator it=myvector.begin(); it!=myvector.end(); ++ it) cout << " " << * it; cout << endl; return 0 ; }
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
