I am trying to write a C++ solution for a problem which is written C, I have learnt C++ very long ago(may be 15 years), and the way C++ is evolved
now is kind of taking long to understand and not easy as 'C' to develop a solution.
Would request any experts help in solving the problem .
Actually, the problem is quite big , but i broke down into small , so i that i can reuse from learnings here.
I have already started with a small class here to store a list of strings and print them.
But what i need is some good C++ way to store pre-defined enities and then create a mapper dynamically based on these entities.
for example in C i can do it this way
enum e_countries {GGG=0,AAA,WWW,FFF,III};
typedef struct Country_t{
char name[SIZE];
char flag[SIZE];
char head[SIZE];
enum e_countries e;
}Country;
Country listOfCountries[] = {
{"GGGG","hjkkd","hhkad",GGG},
{"AAAA","OOO","Ojkjs",AAA},
{"WWWW","Ocghaa","Wahja",WWW},
{"FFFF","Dddaa","Hahada",FFF},
{"III","Masadaa","Iahjda",III},
};
// Create Mapper , for easy and fast accessing of elements based on key
hashsum = calc_hashsum(listOfCountries[cindex].name);
key = hashsum % (numOfCountries);
MakeCountryMapperHashTable(key,&listOfCountries[cindex],numOfCountries);
// some of methods i can use on mapper later
IsConutryUnique(nameofsomecountry);
hashsum = calc_hashsum(nameofsomecountry);
key = hashsum % (numOfCountries);
return strcmp(nameofsomecountry,GetCountry(key));
C++ Solution:
#include <list>
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
class Country
{
private:
std::string name;
std::string flag;
std::string head;
public:
Country(std::string countryName, std::string flagName,std::string headName) :
name(countryName), flag(flagName), head(headName)
{
}
Country();
~Country();
void Print();
};
std::list<Country> listofCountries ={ Country("Country1","Flag1","Head1"), Country("Country2", "Flag2","Head2"), Country("Country3", "Flag3","Head3")};
Country::Country(){}
Country ::~Country(){}
void Country::Print()
{
std::list<Country>::iterator it;
for (it = listofCountries.begin(); it != listofCountries.end(); it++)
{
std::string name = it->name;
std::string flag = it->flag;
std::string head = it->head;
//Print the contents
std::cout <<" C:" << name <<" F:" << flag <<" H:" << head << std::endl;
}
}
int main()
{
Country P1;
P1.Print();
return 0;
}
The class needs to support these
1) Cretae a map with the key as country name
2) Strore Unique Elements:
i want to have country names unique, so i will check the new country with
all the existing countries , and store only if does not existing