• Welcome to Valhalla Legends Archive.
 

Deleting and editing txt files

Started by MoNksBaNe_Agahnim, December 08, 2003, 02:00 PM

Previous topic - Next topic

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

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.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

Skywing

Quote from: Zakath on December 08, 2003, 02:15 PM
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.
I would suggest using STL, which will do all of that for you.

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.


std::map<std::string, std::string> personMap;
std::string name, address;
while ( file >> name >> address )
{
    personMap[name] = address;
}
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.
Eibro of Yeti Lovers.

Skywing

#4
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.

DarkMinion


Adron



Etheran

any binary search is o(log2 n) and it doesn't have to be in a tree ^^

iago

Quote from: Etheran on December 08, 2003, 05:39 PM
any binary search is o(log2 n) and it doesn't have to be in a tree ^^

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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

Quote from: Etheran on December 08, 2003, 05:39 PM
any binary search is o(log2 n) and it doesn't have to be in a tree ^^
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.