# Thread: List stuff again

1. ## List stuff again

Hello again...

I have a different list issue this time. My program generates 20 random numbers that range from 0 to 4 and outputs them to the screen. It then uses push_back to put each number in a list.

After this, I loop through my count function and count how many times each number appears in the list and output this to the screen.

For this problem, the book requires that I use their author-created function called seqSearch (that I will provide as well). Its arguments are the <listname>.begin(), <listname>.end, and whatever we're searching for in the list. The problem I am running into is that I can get it to go and issue the seqSearch once for each number and give me a count of 1 for each numeral, but I can't seem to devise a good loop to get it to count all instances of a number in the list.

Here's my code:

Code:
```#include <iostream>
#include <list>
#include <string>

#include "d_util.h"  // included for the writeList function
#include "d_random.h" // for random number generation
#include "d_search.h" // for seqSearch

template <typename T>
int count(const list<T>& aList, const T& item);

int main()
{
int numList[20];
int i,j;

randomNumber rnd;
list<int> intList(numList, numList+20);

std::cout << "Here are the random numbers: " << endl;

for (i=0;i<20;i++)
{
numList[i]=rnd.random(5);
std::cout<< numList[i] << endl;
intList.push_back(numList[i]);
}

for (j=0;j<5;j++)
{
std::cout << "Here's the count of " << j << ": " << count(intList,j) << endl;
}

return 0;

}

template<typename T>
int count(const list<T>& aList, const T& item)
{
list<T> newList;
newList=aList;
int matchCount=0;
list<T>::iterator iter;

iter=seqSearch<T> (newList.begin(), newList.end(), item);
if (iter!=newList.end())
{
matchCount++;
iter=seqSearch<T> (newList.begin(), newList.end(), item);
}
return matchCount;
}```
Here's the author provided function called seqSearch:

Code:
```template <typename T>
list<T>::iterator seqSearch(list<T>::iterator first,
list<T>::iterator last, const T& target);

template <typename T>
list<T>::iterator seqSearch(list<T>::iterator first,
list<T>::iterator last, const T& target)
{
// start at location first
list<T>::iterator iter = first;

// compare list elements with item until either
// we arrive at last or locate item
while(iter != last && !(*iter == target))
iter++;

// iter either points at item or is last
return iter;
}```
Any guidance that anyone could give would be great!

Thanks!

2. Code:
```
//---------------------------------------------------------------------------
#include <list>
#include <iostream>
#include <vector>
#include <stdlib.h>

using std::list;
using std::vector;
using std::cout;
//---------------------------------------------------------------------------
template <class T> class Finder {
typedef vector<int> TIntVector;
TIntVector counter;
public:
Finder( int n ): counter( n, 0 ) {};
void DoFind( const T& lst ){
T::const_iterator stop=lst.end();
for( T::const_iterator iter=lst.begin(); iter!=stop; ++iter ){
counter[ *iter ]+=1;
}
}
void Print( ostream& os ){
TIntVector::iterator stop=counter.end();
int j=0;
for( TIntVector::iterator iter=counter.begin(); iter!=stop; ++iter, ++j ){
os<< "Here's the count of " << j << ": " << *iter << endl;
}
}
};

int main(int argc, char* argv[])
{
list<int> rlist;
const int NumLimit=5;
for( int i=1; i<=20; i++ ){
rlist.push_back( rand()%NumLimit );
}

Finder< list<int> > finder(NumLimit);
finder.DoFind( rlist );
finder.Print( cout );
return 0;
}
//---------------------------------------------------------------------------```

3. Thanks for that information... However, the parameters of the problem call for me to use the seqSearch templated function that I included in my original post. I'd like to understand how to do it that way rather than re-write it in a different manner.

Since the seqSearch function stops at the first element that matches the search criteria each time, I am not sure how to get it to do what I want. What I'd like it to do (in pseudocode, please forgive me) is:

do seqSearch and try and match the criteria
if it finds a match, keep searching down the list until it either finds another instance of the matching criteria. (and increment my counter when it finds a match). Otherwise, go to the end of the list and be done and return the count from my count function.

The problem I have is that the seqSearch stops at the first element and (from I can tell) the only arguments I can pass it for the beginning range and end range are <listname>.begin() and <listname>.end() which means it'll never find a 2nd instance of the matching criteria if one exists.

If someone could assist me with a way to get around this obstacle, I could figure it out...

Thanks again!

4. You don't have to pass begin() to seqSearch. You can pass any iterator. So when it finds one instance, it returns an iterator pointing at that instance. Just call it again with that iterator (or actually that iterator plus one).

That's part of the design of seqSearch - you don't have to always pass begin() and end(). You can pass it any two iterators as long as the first one comes before the second.

5. I think that will help tremendously.. thanks Jlou! I'll have to look at that in more detail once I get home (I am posting from work right now).. It seemed like an impossible task if the seqSearch couldn't take other beginning and ending ranges..

Thanks!

Popular pages Recent additions