gavin2010 0 Newbie Poster

here is the assignment....im suppost to write 4 functions which are :

bool operator==(const T) const;
bool operator< (const Frequency) const;
template<typename T>
vector<Frequency<T> > Distribution(const vector<T>& v);
template<typename T>
ostream& operator<<(ostream& out, const vector<Frequency<T> >& v);

my code is at the very bottom

Write a template function that will process the elements of a vector and determine the frequency of
each element. Each unique element and its frequency should be stored in an object of the class as specified
in Figure 1. Maintain a vector of such objects. For each element in the vector, first determine if an entry
for that element exists in the vector. Use the STL algorithm find to facilitate this determination. If an
entry has previously been added to the vector, increment its frequency. If the element hasn’t previously been
added, add a new entry to the back of the vector.
A sample main function for testing your functions is shown in Figure 2. Commands to compile, link,
and run this assignment are shown in Figure 3. To use the Makefile as distributed in class, add a target of
lab25main to targets1srcfile.

#ifndef LAB25_H
 #define LAB25_H

 template<typename T>
 class Frequency
 {
 public:
 Frequency(T val) : value(val), frequency(1)
 {}

 void Increment()
 { ++frequency; }

 T GetValue() const
 { return value; }

 uint GetFrequency() const
 { return frequency; }

 bool operator==(const T) const;
 bool operator< (const Frequency) const;
 private:
 T value;
 uint frequency;
 };

 #endif
#include <iostream>
 #include <cstdlib>
 #include <vector>
 #include <lab25.h>

 using namespace std;

 template<typename T>
 vector<Frequency<T> > Distribution(const vector<T>& v);

 template<typename T>
 ostream& operator<<(ostream& out, const vector<Frequency<T> >& v);

MY CODE:

// Gavin Moellendorf
// CS 2305
// Lab 25

#include <algorithm>

template<typename T>
vector<Frequency<T> > Distribution(const vector<T>& v)
{
	vector<Frequency<T> > dist;
	typename vector<T>::const_iterator itr;
	typename vector<Frequency<T> >::iterator findItr;
	
	for(itr = v.begin(); itr < v.end(); ++itr)
	{
		findItr = find(dist.begin(), dist.end(), *itr);
		if(findItr == dist.end())
		{
			Frequency<T> newFreq(*itr);
			dist.push_back(newFreq);//this is how to add a frequency and push it back to the back of the vector
		}
		else
			findItr->Increment();//the arrow derefences whats on the left and then selects whats on the right
	}
	
	return dist;
}

template<typename T>
ostream& operator<<(ostream&out, const vector<Frequency<T> >& v)
{
	return out << GetFrequency();
}

template<typename T>
bool Frequency<T>::operator==(const T itemtoFind) const
{
	return->GetValue() == itemToFind;
}

template<typename T>
bool Frequency<T>::operator< (const Frequency Item) const
{
	return->GetFrequency < GetItem;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.