Going back from C# to C++

Started by
6 comments, last by Sijmen 19 years, 5 months ago
If you want so, you can skip this boring background and head to the question. Quite some time ago I stopped using C++ for a while and started doing some/much programming in C#, using SDL.NET and later my own library ManagedGL. As I got interested in 3D more and more, and the C++ code in the 'MUD Game Programming' book looked really appealing again, I recently partially switched back to C++. But now there's a problem: I never really used the C++ standard library besides some basic cin and cout. Even worse, I was actually coding C+classes, mainly using cstdlib. Using the NeHe tutorials I game quite far, and it looks promising. Now I'm able to draw textured quads and such, I'm going to make a simple maze game - which generally really are 2D besides the graphics, which is nice, because I do know how to make games in general. - question The actual problem: string parsing. Let's say I have this file:

WWWWWWWWWWW
W*........W
WWWWWWW...W
W...@.....W
WWWWWWWWWWW
This is my map. A dot is nothing, a W is a wall, the @ is the start position, and the * is the end position, very simple, so to say. In C#, I'd do it like this:

for(int y=0; y<mapHeight; y++)
	string line = theFileReader.ReadLine();
	for(int x=0; x<mapWidth; x++)
		map[x,y] = createTheTileFromChar(line[x]);
I know how to change this array to a single-dimension array using the y*w+x thingy, but it's really about the string parsing - if you could call it so. How to do this in C++?, using std::string? Then, I'd like to know in what ways these functions are available in C++: TextStream.ReadLine() with std::istream String.Trim() with std::string String.Split() with std::string Thanks!
Advertisement
istream reference -> GetLine() (great reference site for standard C++ functions)

What are those other functions? Browse that website, you might find a similar function.

strtok(), kinda like String.Split().
You'd be better off writing these functions yourself, very fast:

vector<string> Split(std::string str, char del) {    vector<string> strVector;    string temp;    for(int i=0; i<str.length(); i++) {        if(str == del) {            strVector.push_back(temp);            temp = "";            continue; // next loop-round immediately        }        temp += str;    }    return strVector;}


Something like this, not sure if that vector<> part will work, though.
Don't use istream getline like Pipo suggested, it's for char*. Use "std::getline(inputStream, string);"

And for split and trim, see latest version of Boost and it's String Algorithms Library:
http://www.boost.org/doc/html/string_algo.html
Thanks for the info. That'll get me going.

/edit:

How would you implement that map loading thing? Using a filestram or something lik that? Then how to read a single character from it?
Thinking in C++ Volume II
You'll soon discover that string manipulation in C++ isn't anywhere near as nice as it is in C# [smile]

Reading a line is easy:
std::istream in_stream;std::string out_string;std::getline(in_steam,out_string);


String.Trim() can be done like this:
std::string str;str = str.substr(0,str.find_last_not_of(' '));str = str.substr(str.find_first_not_of(' '));


and String.Split() is a little trickier. You can either get a char* pointer to it's contents with str.c_str() and use strtok() as mentioned (be careful though, the pointer returned by str.c_str() is only guarentteed to be valid if str isn't modified). Or alternatively you could do it like so:
std::list<std::string> split(const std::string& str, const std::string& separators){    std::list<std::string> tokens;    for(std::string::size_type index=0 ; index!=std::string::npos ;)    {        std::string::size_type first = str.find_first_not_of(separators,index);        std::string::size_type index = str.find_first_of(separators,first);        if(first!=std::string::npos) tokens.push_back(str.substr(first,index-first));    }    return tokens;}


EDIT: Wow, I'm slow, there was only 1 reply when I started typing this! [crying]
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Wow, thanks all! That's very nice. This is more than enough to get me started for sure.

This topic is closed to new replies.

Advertisement