Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 118,923 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,945 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Using the STL algorithm: fill ()

 
Reply to this topicStart new topic

> Using the STL algorithm: fill (), #include <algorithm>

gabehabe
Group Icon



post 19 May, 2008 - 11:45 AM
Post #1


What do I need to know before studying this tutorial?
You should have a knowledge of vectors and vector iterators

First off, what does fill() do?
Well, fill() does exactly what it says on the tin. It fills a vector with a specific value.

Before we begin, here is the general syntax for using the fill function:
fill (<where to begin filling from>, <where to stop filling>, <what to fill with>);

What do we need to include?
cpp
#include <iostream> // output the values to see what's going on
#include <algorithm> // our algorithm library
#include <vector> // we need a vector to use the algorithms on

using namespace std; // vectors and output

Next, we need to begin our int main () function. I'm gonna assume you know how to do this, and move on tongue.gif

Now we need to declare a vector and a vector iterator:
cpp
vector <int> myVec; // create a vector called myVec
vector <int> :: iterator it; // create a vector iterator called it


And then we want to fill and print the vector. Easy enough:
cpp
for (int i = 0; i < 9; ++i)
myVec.push_back(i); // fill the vector with some values

cout << "Before fill: ";
for (it = myVec.begin(); it != myVec.end(); ++it)
cout << *it << " "; // print the contents of myVec

Now for our "fill" function.
cpp
fill (myVec.begin(), myVec.end(), 5); // fill all values with the value '5'

All we need to do now is output the contents of the vector, and exit the program:
cpp
cout << endl << "After fill: ";
for (it = myVec.begin(); it != myVec.end(); ++it)
cout << *it << " "; // print the contents of myVec again

cin.get (); // pause
return EXIT_SUCCESS; // everything went OK


Here's the complete code, for those of you who learn better by example alone:
cpp
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main ()
{
vector <int> myVec; // create a vector called myVec
vector <int> :: iterator it; // create a vector iterator called it

for (int i = 0; i < 9; ++i)
myVec.push_back(i); // fill the vector with some values

cout << "Before fill: ";
for (it = myVec.begin(); it != myVec.end(); ++it)
cout << *it << " "; // print the contents of myVec

fill (myVec.begin(), myVec.end(), 5); // fill all values with the value '5'

cout << endl << "After fill: ";
for (it = myVec.begin(); it != myVec.end(); ++it)
cout << *it << " "; // print the contents of myVec again

cin.get (); // pause
return EXIT_SUCCESS; // everything went OK
}
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Amadeus
Group Icon



post 27 May, 2008 - 07:19 AM
Post #2
This tutorial has been approved, as the information is factually correct. This, with other submissions, however, are really examples of some of the basic features of the algorithm header. Perhaps a more encompassing tutorials grouping several methods together might be beneficial.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/13/08 05:13AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month