Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Data structures

Sign in to follow this  


I'm taking a data structures class this semester and I quite like it. The programs, for me at least, have been really easy (queues, and a hash table implementation), others in the class seem to be struggling somewhat. This isn't to brag, but a lot of them seem to think that they can code up anything an hour before it's due and of course, most of them don't make it.

Ah well, I started on the next project today, which I should have done rather soon since I can reuse a lot of the code from the previous project, just implementing a different data structure. Today I coded up a small parser that I was happy with to sort of tokenize a string into its various parts.

Given a string with the following format:


the function returns each part. It seems to me that there would be ways already implemented to do this sort of thing with strings, but I haven't found it. It was a nice exercise at least. I'm sure it's not the most efficient, but it's easy to use and gets the job done. If you can use it, by all means, have at it.

string getToken(string& initial, char delim)
string temp = "";
int length = (int)initial.size();
for(int i=0; i {
temp += initial.at(i);
if(initial.at(i) == delim)
initial.erase(0, i+1);
temp.erase(i, 1);
return temp;
return temp;

Sign in to follow this  


Recommended Comments

Can I suggest you to use the std::string::find() method ? It seems to be what you are looking for :)

#include <algorithm>
#include <string>

std::string getToken(std::string& initial, char delim)
std::string result;
// find the position of the next delimiter. One can also
// try to cope with more delimiters using std::string::find_first_of()
std::string::size_type position = initial.find(delim);
// don't care if the position is std::string::npos because
// if there is no delimiter, then we copy everything into result
result = initial.substr(0, position);
// remove the used characters from the initial string
if (position == std::string::npos) {
// we are eating all the string in this case (no delimiter found)
initial = "";
} else {
// we eat every used characters - including the delimiter
initial.erase(initial.begin(), initial.begin()+position+1);
return result;

Share this comment

Link to comment
Also, std::string::find_first_of() and std::string::find_last_of() are very useful for this kind of stuff. I like to use find_last_of to strip the extensions off filenames.

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!