This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #467 from kbodurri/fill_n
Explaining the fill_n function
- Loading branch information
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# fill_n | ||
|
||
**Description** : Sets a number of elements of a range (i.e, vector) to a value. | ||
The function takes 3 arguments. The iterator of the range, the number of elements | ||
to set starting from the iterator's first element and the new value. | ||
|
||
**Example** : | ||
```cpp | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
void print_vector(const std::vector<int> &v) { | ||
// iterate through v and print its elements | ||
for(auto &elem:v) { | ||
std::cout << elem << " "; | ||
} | ||
std::cout<<std::endl; | ||
} | ||
|
||
int main() { | ||
std::vector<int> v = {1,1,1,1,1,1,1,1}; | ||
|
||
std::cout << "Before fill_n: "; | ||
print_vector(v); | ||
|
||
// set the first half of v's elements to zero | ||
std::fill_n(v.begin(), v.size()/2, 0); | ||
|
||
std::cout << "After fill_n: "; | ||
print_vector(v); | ||
return 0; | ||
} | ||
``` | ||
**[Run Code](https://rextester.com/CIZQZ21473)** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Author : Klajdi Bodurri | ||
Date : 08/01/2020 | ||
Time : 14:00 | ||
Description : Show an example of fill_n | ||
*/ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
void print_vector(const std::vector<int> &v) { | ||
// iterate through v and print its elements | ||
for(auto &elem:v) { | ||
std::cout << elem << " "; | ||
} | ||
std::cout<<std::endl; | ||
} | ||
|
||
int main() { | ||
std::vector<int> v = {1,1,1,1,1,1,1,1}; | ||
|
||
std::cout << "Before fill_n: "; | ||
print_vector(v); | ||
|
||
// set the first half of v's elements to zero | ||
std::fill_n(v.begin(), v.size()/2, 0); | ||
|
||
std::cout << "After fill_n: "; | ||
print_vector(v); | ||
return 0; | ||
} |