Computer Science Canada Need some C++ Help |
Author: | jin [ Thu Oct 07, 2010 11:48 pm ] |
Post subject: | Need some C++ Help |
Hi Guys, so its been a while since i have programmed anything in c++ so i just have a few performance questions about my assignment. for the assignment i need to parse a file and store form every line an email, first name, middle name and last name and then do some comparisons. its been a while and i cant quite remember the pros and cons so 1. what would be better to use a struct with 4 string fields or just use a 2d string array? 2. i also need to sort based on a field (last name) so which would be the best algorithm to use? quick, merge, heap? in my testing ill prolly only have 20-50 lines but when being marked they may have 1000s. thanks |
Author: | DtY [ Fri Oct 08, 2010 6:30 am ] |
Post subject: | RE:Need some C++ Help |
A struct would be better for this, that way you can do something like person->firstname, rather than person[2] which means nothing. I can't say for the second which would be best. |
Author: | OneOffDriveByPoster [ Sun Oct 10, 2010 10:02 pm ] |
Post subject: | Re: Need some C++ Help |
A class is a better idea because you won't have to move the other fields to match the one you are sorting on. You should also be moving pointers to the class objects instead of the actual objects. |
Author: | TheGuardian001 [ Sun Oct 10, 2010 10:10 pm ] |
Post subject: | Re: Need some C++ Help |
OneOffDriveByPoster @ Sun Oct 10, 2010 10:02 pm wrote: A class is a better idea because you won't have to move the other fields to match the one you are sorting on.
What do you mean by this? IIRC, the only difference between a class and a struct in C++ is that Struct members are public by default, while Class members are private by default. |
Author: | OneOffDriveByPoster [ Sun Oct 10, 2010 10:27 pm ] |
Post subject: | Re: Need some C++ Help |
TheGuardian001 @ Sun Oct 10, 2010 10:10 pm wrote: OneOffDriveByPoster @ Sun Oct 10, 2010 10:02 pm wrote: A class is a better idea because you won't have to move the other fields to match the one you are sorting on.
What do you mean by this? IIRC, the only difference between a class and a struct in C++ is that Struct members are public by default, while Class members are private by default. The reason I gave for using the "struct" option was additional to the one given by DtY. As for using the "class" versus "struct" keywords, you are correct that they can both be used as a class-key for non-union class types in C++. There are some additional differences between struct and class keywords (for example, "class" has a special meaning in a template parameter list). |