STL Map

Started by
18 comments, last by brain21 16 years, 11 months ago
Hi, I'm trying to make a resource manage for a small game I'm making. I'm having problems with maps. This is my code:

#include <stdio.h>
#include <string.h>

#include <map.h>

map< char*, int >Name;

char TempName[256];

void p( char *n )
{
	printf( "%s = %d\n", n, Name[ n ] );
}

int main()
{
	Name.clear();

	Name[ "Denis" ] = 29;

	char n[] = "Denis";
	strcpy( TempName, "Denis" );

	p( "Denis" );
	p( n );
	p( (char*)TempName );
	p( "Denis" );

	return 0;
}



This is what I get as output: Denis = 29 Denis = 0 Denis = 0 Denis = 29 So how do I get the two middle ones to work. In my game I'll be reading filenames from a text file and adding them to the map, but it doesn't look like it will work. Can someone see what I'm doing wrong? Brain21
Advertisement
use std::string instead of char* . The map will compare the pointers, not the contents of the pointers ( unless you give it a different comparator). So just replace char* with string.
You compare pointers, not strings.
I recommend using std::string instead of char*
As others have said, use a std::string. Also, the correct headers in C++ are <cstdio>, <string>, and <map> (without the .h).
Well, as already said above, you've just found out just *why* C++ has a proper string class, instead of relying on char pointers. Because char pointers don't behave like strings, and so, shouldn't be *used* as strings.

Your code also looks like an awful mishmash of C and C++. Check the comments I added:

#include <stdio.h> // cstdio is the equivalent C++ header for using C-style IO. For C++ style I/O (recommended) use the header iostream instead.#include <string.h> // the C++ header is just called string. Not string.h#include <map.h> // The C++ header is just map, with no .hmap< char*, int >Name; // As already said, use std::string, not char*char TempName[256]; // And again. std::stringvoid p( char *n ) // And here.{	printf( "%s = %d\n", n, Name[ n ] ); // use std::cout instead of printf. It's C++, and it's a hell of a lot safer to use.}int main(){	Name.clear();	Name[ "Denis" ] = 29;	char n[] = "Denis";	strcpy( TempName, "Denis" ); // strcpy is for char*'s. When working with strings, you don't need that, you can just use the assignment operator (=)	p( "Denis" );	p( n );	p( (char*)TempName ); // Try to avoid casts. They're generally not neccesary. Also in C++, use static_cast and reinterpret_cast instead of the C-style cast	p( "Denis" );	return 0;


[smile]
ok thanks guys. I will use strings from now on. I don't use that cout thing cause I don't know how to use them. I've looked at tutorials before and followed along, but my compiler kept giving me tons of error messages with <> and :: in it that I didn't understand. Some messages were several lines long. A little uch for me right now. I'll keep trying.

Thanks,
Brain21
Using cout is ridiculously easy. (much easier than printf, since you don't have to remember all the formatting codes like %s)

translating your printf call into cout would look like this:

#include <iostream>std::cout << n << " = " << Name[ n ] << std::endl;

(std::endl inserts a newline and flushes the string stream. If you only want to insert the newline, you can just do "\n" instead

Edit: Doh, fixed a typo. Thanks jfl [grin]

[Edited by - Spoonbender on May 13, 2007 12:46:32 PM]
Quote:Original post by Spoonbender
#include <iostream>std::cout << n << " = " << Name[ n ] << std::endl;

std::endl inserts a newline and flushes the stream.


Fixed.


jfl.
ok, thanks, I'll try that. What about the iostream stuff for files instead of fopen, and related functions? How easy is it to use those?
Quote:Original post by brain21
ok, thanks, I'll try that. What about the iostream stuff for files instead of fopen, and related functions? How easy is it to use those?

Very [smile]

So easy that you can abstract away from the type of stream you are writing to (and because of the nature of C++, you can make modifications to streams to allow more esoteric types, such as a network stream for example).

This is how to create a file stream (stupid examples):
#include <fstream>void writeToFile( const std::string &str ){   ofstream file("out.txt"); // ofstream is output file stream   file << str;   // file closes itself automatically}std::string readFromFile(){   ifstream file("in.txt");   std::string text;   file >> text; // gets the first word   // or use std::getline(file,text); to get a full line   return text;}//////////////////////////////////////////////////#include <iosfwd>// output to some generic stream, type unspecifiedvoid writeToSomeStream( std::ostream &stream, const std::string &str ){    stream << str;}// usage:void foo(){    writeToSomeStream( std::cout, "Hello std::cout!\n" );    std::ofstream out("out.txt");    writeToSomeStream( out, "Hello little file object\n" );}//////////////////////////////////////////////////#include <iosfwd>std::string readFromSomeStream( std::istream &stream ){    std::string text;    stream >> text;    return text;}// usage:void foo(){    std::string text = readFromSomeStream( std::cin );    std::ifstream in("in.txt");    text = readFromSomeStream( in );}// or the ever useful, the string streamvoid bar(){    std::streamstream stream;    stream << "Hello user how are you";    std::string text;    text = readFromSomeStream(stream); // text == Hello    text = readFromSomeStream(stream); // text == user    text = readFromSomeStream(stream); // text == how    text = readFromSomeStream(stream); // text == are    text = readFromSomeStream(stream); // text == you}


Note, I apologise for any errors in the above, I haven't compiled or tested it.

This topic is closed to new replies.

Advertisement