Author | Message | Time |
---|---|---|
MoNksBaNe_Agahnim | I was wondering... I have a c++ competition coming up and what I am needing to do it input info into a txt file, like a name and address, and then be able to edit and delete entries. All the users are going to be compiled into one txt file, and I already know how to put them into a file. And if possible is there a way to assort them alphabetically. thanks | December 8, 2003, 8:00 PM |
Zakath | I'd suggest loading the user entries into a linked list, and running some sort of sorting algorithm on the list. Then, once you've got a sorted list, overwrite the file and write each record from the list into the file, so that they'll now be in sorted order. | December 8, 2003, 8:15 PM |
Skywing | [quote author=Zakath link=board=30;threadid=4153;start=0#msg34316 date=1070914531] I'd suggest loading the user entries into a linked list, and running some sort of sorting algorithm on the list. Then, once you've got a sorted list, overwrite the file and write each record from the list into the file, so that they'll now be in sorted order. [/quote] I would suggest using STL, which will do all of that for you. | December 8, 2003, 8:15 PM |
Eibro | The STL is your best friend for situations like this. std::map is a sorted associative container, it is most likely what you're looking for. [code] std::map<std::string, std::string> personMap; std::string name, address; while ( file >> name >> address ) { personMap[name] = address; } [/code]Like I said, get familiar with the STL. It is HUGE, but the more of it you know, the quicker you can solve little problems like this. | December 8, 2003, 8:18 PM |
Skywing | Also note that std::set, std::multiset, std::map, and std::multimap are highly optimized for fast lookups on sorted data. Typically, these are implemented as binary trees with log2 worst case search times. Note that you are guaranteed O(log2(n)) search speed for the above containers by the C++ standard - any conforming STL implementation must have at least O(log2(n)) search speed. | December 8, 2003, 8:21 PM |
DarkMinion | [quote]std::string[/quote] Eww! | December 8, 2003, 8:31 PM |
Adron | Why is this sticky? | December 8, 2003, 8:37 PM |
Yoni | [quote author=Adron link=board=30;threadid=4153;start=0#msg34329 date=1070915844] Why is this sticky? [/quote]hmm fixed | December 8, 2003, 8:40 PM |
Etheran | any binary search is o(log2 n) and it doesn't have to be in a tree ^^ | December 8, 2003, 11:39 PM |
iago | [quote author=Etheran link=board=30;threadid=4153;start=0#msg34355 date=1070926763] any binary search is o(log2 n) and it doesn't have to be in a tree ^^ [/quote] There are faster ways than a binary tree, though. A Balanced-tree (b-tree), 2-3 tree, or an n-tree, is faster. I forget how to implement them, though.. they were pretty confusing when I learned them. | December 8, 2003, 11:46 PM |
Skywing | [quote author=Etheran link=board=30;threadid=4153;start=0#msg34355 date=1070926763] any binary search is o(log2 n) and it doesn't have to be in a tree ^^ [/quote] STL provides performance requirements, not implementation requirements. The container representation and the search algorithm can be whatever the implementer wants so long as they meet C++'s performance requirements. | December 9, 2003, 12:27 AM |