Template Iterator Algorithm
Hi. I am trying to figure out how to get this program to remove the item I am commanding (70 in this case). I am obviously barking up a slightly wrong tree here. I have just started working with iterators. Could someone give some helpful hints? Thanks.
Code:
#include<iostream>
#include<iterator>
#include<vector>
#include<string>
using namespace std;
template <class T>
class Set
{
public:
void add(const T& anItem);
void remove(const T& anItem);
friend ostream& operator <<(ostream& os, Set<T>& aSet)
{
typename vector<T>::iterator anIt;
for(anIt=aSet.theMembers.begin();anIt != aSet.theMembers.end();anIt++)
{
os<<(*anIt)<<" ";
os<<endl;
}
return os;
}
private:
vector<T> theMembers;
};
int main()
{
Set<int> s1;
s1.add(100);
s1.add(90);
s1.add(80);
s1.add(70);
s1.add(60);
s1.add(50);
cout<<s1<<endl;
s1.remove(70);
cout<<s1<<endl;
system("pause");
return 0;
}
template <class T>
void Set<T>::add(const T& anItem)
{
theMembers.push_back(anItem);
}
template<class T>
void Set<T>::remove(const T& anItem)
{
typename vector<T>::iterator anIt;
for(anIt=theMembers.begin();anIt != theMembers.end();anIt++)
{
if((*anIt)==anItem)
(*anIt)=((*anIt)++);
}
theMembers.pop_back();
}