STL algorithm算法fill_n(15)

原文地址:http://www.cplusplus.com/reference/algorithm/fill_n/

function template
<algorithm>

std::fill_n

template <class OutputIterator, class Size, class T>
  OutputIterator fill_n (OutputIterator first, Size n, const T& val);
Fill sequence with value
Assigns val to the first n elements of the sequence pointed by first.
从first开始,将之后n个元素的值变为val.
例子:
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;

void filln(){
    vector<int> vi{1,5,7,8,9,9,8,5,9};
    cout<<"at first,vi=";
    for(int &i:vi)
        cout<<i<<" ";
    cout<<endl;

    auto it=fill_n(vi.begin()+3,4,1000);
    cout<<"after     fill_n(vi.begin()+3,4,1000),\nvi=";
    for(int &i:vi)
        cout<<i<<" ";
    cout<<endl;
    cout<<"it="<<*it<<endl;

}



运行截图:


The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
template <class OutputIterator, class Size, class T>
  OutputIterator fill_n (OutputIterator first, Size n, const T& val)
{
  while (n>0) {
    *first = val;
    ++first; --n;
  }
  return first;     // since C++11
}



Parameters

first
Output iterators to the initial position in a sequence of at least n elements that support being assigned a value of type T.
开始的位置。
n
Number of elements to fill.
If negative, the function does nothing.
Size shall be (convertible to) an integral type.
要被调整的元素个数。(如果是负数,不会做任何事情)
val
Value to be used to fill the range.
要填充的值。

Return value

An iterator pointing to the element that follows the last element filled.

返回值为一个指向最后一个被填充的元素的下一个元素的位置。


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// fill_n example
#include <iostream>     // std::cout
#include <algorithm>    // std::fill_n
#include <vector>       // std::vector

int main () {
  std::vector<int> myvector (8,10);        // myvector: 10 10 10 10 10 10 10 10

  std::fill_n (myvector.begin(),4,20);     // myvector: 20 20 20 20 10 10 10 10
  std::fill_n (myvector.begin()+3,3,33);   // myvector: 20 20 20 33 33 33 10 10

  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}


Output:
myvector contains: 20 20 20 33 33 33 10 10

Complexity

Linear in n: Assigns a value to each element.

Data races

The n first objects at the range pointed by first are modified (each object is modified exactly once).

Exceptions

Throws if either an element assignment or an operation on an iterator throws.
Note that invalid arguments cause undefined behavior.


——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-11

于GDUT

——————————————————————————————————————————————————————————————————




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值