fill對(duì)區(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可 指定填充的個(gè)數(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為要填充的元素個(gè)數(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號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
