📄 fill_n.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>fill_n</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C++ Algorithms</a> > <a href= "fill_n.html">fill_n</a> </div> <div class="name-format"> fill_n </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <algorithm> #include <algorithm> iterator fill_n( iterator start, size_t n, const <a href="../containers.html">TYPE</a>& val );</pre> <p>The fill_n() function is similar to (C++ I/O) <a href= "../cppio/fill.html">fill</a>(). Instead of assigning <em>val</em> to a range of elements, however, fill_n() assigns <em>val</em> to the first <em>n</em> elements starting at <em>start</em>.</p> <p>For example, the following code uses fill_n() to assign -1 to the first half of a vector of integers:</p> <pre class="example-code"> vector<int> v1; for( int i = 0; i < 10; i++ ) { v1.push_back( i ); } cout << "Before, v1 is: "; for( unsigned int i = 0; i < v1.size(); i++ ) { cout << v1[i] << " "; } cout << endl; fill_n( v1.begin(), v1.size()/2, -1 ); cout << "After, v1 is: "; for( unsigned int i = 0; i < v1.size(); i++ ) { cout << v1[i] << " "; } cout << endl; </pre> <p>When run, this code displays:</p> <pre class="example-code"> Before, v1 is: 0 1 2 3 4 5 6 7 8 9 After, v1 is: -1 -1 -1 -1 -1 5 6 7 8 9 </pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="fill.html">fill</a> </div> </div> </td> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -