
-----------------------------------
jin
Thu Oct 07, 2010 11:48 pm

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

-----------------------------------
DtY
Fri Oct 08, 2010 6:30 am

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.

-----------------------------------
OneOffDriveByPoster
Sun Oct 10, 2010 10:02 pm

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.

-----------------------------------
TheGuardian001
Sun Oct 10, 2010 10:10 pm

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.

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.

-----------------------------------
OneOffDriveByPoster
Sun Oct 10, 2010 10:27 pm

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.

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.I was responding to the OP's question (I should have quoted from that I guess). I did not say that "class was better then struct". I chose to use "class" instead of "struct" to represent the same option. Sorry for the confusion.

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).
