#include <iostream>
#include <string>
#include <conio.h>
usingnamespace std;
template< class T >
class Set{
private:
//Cannot be negative number , validation
staticconst size_t N = 100;
T Item[N];
size_t count;
public:
Set() : count(0){};
~Set();
void addNewItem(const T &);
void RemoveItem(const T &);
size_t size();
size_t max_size();
int FindItem(const T &);
};
template< class T >
void Set<T> :: addNewItem( const T &item){
int result = FindItem(item);
//Add new item when FindItem return a value -1
if( result == -1 ){
item[++N];
}
else
cout << endl;//Nothing Happen
}
template< class T >
void Set<T> :: RemoveItem( const T &item ){
int result = FindItem(item);
if( result >= 0 && result < count ){
item[--N];
}
}
template< class T >
size_t Set<T> :: size() const{
return count;
}
template< class T >
size_t Set<T> :: max_size() const{
return N;
}
template< class T >
int Set<T> :: FindItem( const T &item ){
int result = -1;
for( int i = 0 ; i < count ; i++ ){
if( item == Item[i] ){
result = i;
break;
}
}
return result;
}
int main(){
int choice = 0;
string title;
system( "pause" );//Pause window
cout << "\tSet of items\n--------------------------" << endl
<< "1.Add New Item " << endl
<< "2.Remove an Item " << endl
<< "3.Number of items in the set " << endl
<< "4.Find Item " << endl
<< "5.Get all Items " << endl << endl
<< "Enter Choice : ";
cin >> choice;
switch( choice ){
case 1:{
cout << "Enter title : ";
getline( cin , title );
Set<string> set;
set.addNewItem(title);
break;
}
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
cout << "Invalid Input ! Please re-enter ! "<< endl;
break;
}
system( "pause" );
return 0;//Exit program
}
so my question is if i Add a new item to the set. If the item is already in the set then nothing happens.
and the remove item Remove an item from the set.
but i think my implementation for add and remove totally concept wrong right? might help ? thanks