C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 309,260 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,488 people online right now. Registration is fast and FREE... Join Now!




> STL algorithm::count(), #include <algorithm>

gabehabe
Group Icon



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


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

What does the count() algorithm do?
It counts the # of a specified value within a vector

The syntax for the count() algorithm:
int count = count(<where to begin searching>, <where to stop searching>, <value to search for>);

What to include:
cpp
#include <iostream> // just to output what's going on
#include <algorithm> // algorithms!
#include <vector> // vector to use algorithms on

using namespace std;

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

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

We now need a loop to fill our vector with some values. Let's use some random numbers:
cpp
for (int i = 0; i < 9; ++i)
{
int random = rand () % 5; // generate a random number
myVec.push_back(random); // fill the vector with some values
}

Let's print the contents:
cpp
cout << "Contents: ";
for (it = myVec.begin(); it != myVec.end(); ++it)
cout << *it << " "; // print the contents of myVec


Next is the count function. Ready? (Take a look at the syntax at the beginning of this tutorial to compare them)
cpp
int count_value = count (myVec.begin(), myVec.end(), 4);

Now all we need to do is print our count variable and exit the program:
cpp
cout << endl << "Number of 4s stored in vector: " << count_value;

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

For those of you who learn better by example, here is the complete code:
cpp
#include <iostream> // just to output what's going on
#include <algorithm> // algorithms!
#include <vector> // vector to use algorithms on

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)
{
int random = rand () % 5; // generate a random number
myVec.push_back(random); // fill the vector with some values
}

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

int count_value = count (myVec.begin(), myVec.end(), 4);

cout << endl << "Number of 4s stored in vector: " << count_value;

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!

 
Reply to this topicStart new topic
Replies(1 - 1)
Amadeus
Group Icon



post 27 May, 2008 - 06:18 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: 11/26/09 10:08AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month