I am creating a program that stores cities with their geographical points so that I can measure the distance between them. these cities will be stored in derived classes using vectors. ATM I'm having a hard time to try and code ways to prevent a city from being entered twice, I imagine you would use a find() function ? and also how can I delete a city (a specific derived class) ?
the base class will be structured something like this:
class city
{
double latitude, longitude;
char name[255];
};
revealing my whole program will be too much to read and I'm only expecting some sort of guidance. I'm quite new with vectors.
Mmmm, well if you are using the default comparison operator (you might want to define it just in case), then you can use an std::set instead of an std::vector. Read:
the function 'count' should be better that 'find' to check if a value was already entered.
In order to use it, you should provide a == operator for 'city'
eg:
1 2 3 4
booloperator == (const city &a, const city &b)
{
return !strcmp(a.name,b.name) && a.latitude==b.latitude && a.longitude==b.longitude;
}
I tried implementing the example code above that you showed but I get these errors...
in function int main()':
line 1 : expected `,' or `..' before '&' token
line 3 : a function-definition is not allowed here before '{' token
line 3 : expected `,' before '{' token
[Build Error] [main.o] Error 1
1) name should really be a std::string instead of a char array;
2) you can't compare doubles for equality;
3) the default operator== should then suffice;
4) both count() and find() are O(n) so neither is really any better than the other.