C++ 算法 中的 fill_n函数

首页 / C++入门教程 / C++ 算法 中的 fill_n函数

C++算法 fill_n()函数用于为从特定元素开始的参数内的指定数量的元素分配新值。

这意味着在fill_n()中,我们指定开始位置,要填充的元素数和要填充的值。

fill_n - 语法

template <class OutputIterator, class Size, class T>
  void fill_n (OutputIterator first, Size n, const T& val);		//until C++ 11

template <class OutputIterator, class Size, class T>
  OutputIterator fill_n (OutputIterator first, Size n, const T& val);   	//since C++ 11

fill_n - 参数

first:一个输出迭代器,指向要分配值val的参数内第一个元素的位置。

val :用于填充参数的值。

n :要填充的元素数可以是有符号或无符号整数类型。

fill_n - 返回值

fill_n()的第一个版本不返回任何内容,fill_n()的第二个版本返回一个迭代器,该迭代器指向在要填充的最后一个元素之后的元素。

fill_n - 例子1

让我们看一个简单的示例来演示fill_n()的用法:

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-fill-n-function.html

来源:LearnFk无涯教程网

#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

int main() {

  fill_n(ostream_iterator<int>(cout, ","), 10, 3);
  return 0;
}

输出:

3,3,3,3,3,3,3,3,3,3,

fill_n - 例子2

让我们看另一个简单的例子:

无涯教程网

#include <vector>  
#include <algorithm>  
#include <iostream>  
  
int main()   
{  
    using namespace std;  
    vector <int> v;  
  
    for ( auto i = 0 ; i < 9 ; ++i )  
        v.push_back( 0 );  
  
    cout << "  vector v = ( " ;  
    for ( const auto &w : v )  
        cout << w << " ";  
    cout << ")" << endl;  
  
   //Fill the first 3 positions with a value of 1, saving position.  
    auto pos = fill_n( v.begin(), 3, 1 );  
  
    cout << "modified v = ( " ;  
    for ( const auto &w : v )  
        cout << w << " ";  
    cout << ")" << endl;  
  
   //Fill the next 3 positions with a value of 2, using last position.  
    fill_n( pos, 3, 2 );  
  
    cout << "modified v = ( " ;  
    for ( const auto &w : v )  
        cout << w << " ";  
    cout << ")" << endl;  
  
   //Fill the last 3 positions with a value of 3, using relative math.  
    fill_n( v.end()-3, 3, 3 );  
  
    cout << "modified v = ( " ;  
    for ( const auto &w : v )  
        cout << w << " ";  
    cout << ")" << endl;  
    
    return 0;
}  

输出:

  vector v =  ( 0 0 0 0 0 0 0 0 0 )
modified v = ( 1 1 1 0 0 0 0 0 0 )
modified v = ( 1 1 1 2 2 2 0 0 0 )
modified v = ( 1 1 1 2 2 2 3 3 3 )

fill_n - 例子3

让我们看另一个简单的例子:

无涯教程网

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std; 
  
int main() 
{ 
    vector<int> vect(8);   
  
   //calling fill to initialize first four values 
   //to 7 
    fill_n(vect.begin(), 3, 1); 
  
    for (int i=0; i<vect.size(); i++) 
        cout << ' ' << vect[i]; 
    cout << '\n'; 
  
   //calling fill to initialize 3 elements from  
   //"begin()+3" with value 4 
    fill_n(vect.begin() + 3, 3, 4); 
  
    for (int i=0; i<vect.size(); i++) 
        cout << ' ' << vect[i]; 
    cout << '\n'; 
  
    return 0; 
}

输出:

 1 1 1 0 0 0 0 0
 1 1 1 4 4 4 0 0

fill_n - 例子4

让我们看另一个简单的例子:

无涯教程网

#include <vector>
#include <algorithm>
#include <iostream>
 
using namespace std;
 
int main()
{
  vector <int> vec;
  vector <int>::iterator Iter1;
  int i;
  for (i = 10; i <= 20; i++)
    vec.push_back(i);
  cout <<"Vector vec data: ";
  for (Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)
    cout <<*Iter1<<" ";
  cout <<endl;
 //fill the last 3 positions for 6 position with a value of 9
  cout <<"\nOperation: fill_n(vec.begin() + 3, 6, 9)\n";
  fill_n(vec.begin() + 3, 6, 9);
  cout <<"Modified vec data: ";
  for (Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)
    cout <<*Iter1<<" ";
   cout <<endl;
 
  return 0;
}

输出:

Vector vec data: 10 11 12 13 14 15 16 17 18 19 20 

Operation: fill_n(vec.begin() + 3, 6, 9)
Modified vec data: 10 11 12 9 9 9 9 9 9 19 20

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

10x程序员工作法 -〔郑晔〕

性能工程高手课 -〔庄振运〕

Selenium自动化测试实战 -〔郭宏志〕

Django快速开发实战 -〔吕召刚〕

容器实战高手课 -〔李程远〕

分布式金融架构课 -〔任杰〕

体验设计案例课 -〔炒炒〕

超级访谈:对话张雪峰 -〔张雪峰〕

郭东白的架构课 -〔郭东白〕

好记忆不如烂笔头。留下您的足迹吧 :)