using map<char[256], T*>

P

puzzlecracker

I need to be able to use map<char[256], T*>, I can make it
std::map<std::string, T*>, however, Key is passed by char[], hence I
would need to call c_str() all the time. What do you suggest?
 
K

Kai-Uwe Bux

puzzlecracker said:
I need to be able to use map<char[256], T*>, I can make it
std::map<std::string, T*>, however, Key is passed by char[], hence I
would need to call c_str() all the time. What do you suggest?

Huh? The following seems to work:

#include <map>
#include <string>
#include <iostream>
#include <ostream>

typedef char word [256];
typedef std::map< std::string, int > my_map;

int main ( void ) {
my_map the_map;
word key1 = "abc";
word key2 = "xyz";
the_map[ key1 ] = 1;
the_map[ key2 ] = 5;
std::cout << the_map[ key1 ] << '\n';
std::cout << the_map[ key2 ] << '\n';
}

So where exactly is the problem with using std::string as the key_type?


Best

Kai-Uwe Bux
 
E

Erik Wikström

I need to be able to use map<char[256], T*>, I can make it
std::map<std::string, T*>, however, Key is passed by char[], hence I
would need to call c_str() all the time. What do you suggest?

If you can find a really good reason to not use std::string (I doubt it)
you could create a struct like so:

struct Key
{
char str[256];
bool operator<(const Key&) { /* ... */ }
};
 
R

Richard Herring

In message
I need to be able to use map<char[256], T*>, I can make it
std::map<std::string, T*>, however, Key is passed

passed where?
by char[],

which decays to char * -- did you omit "const" in there?
hence I
would need to call c_str() all the time.

Only to convert from string to const char *. There's an implicit
conversion in the other direction.

But why would using c_str() be a problem?
What do you suggest?

Use std::string unless you have a really really good reason not to.
Because of the implicit conversion, with a map<string, T*> you can
interchangeably use std::string or const char * as the parameter to
operator[], find(), count() etc.
 
M

Michael DOUBEZ

puzzlecracker a écrit :
I need to be able to use map<char[256], T*>, I can make it
std::map<std::string, T*>, however, Key is passed by char[], hence I
would need to call c_str() all the time. What do you suggest?

If you have a TR1 implementation, you can use std::tr1::array<char,256>.
It already has all the semantics you need.

If the strings are of different size, then std::string is IMHO the way
to go. If things go wrong you can always revert later on.
 
A

Andrew Koenig

I need to be able to use map<char[256], T*>,

Well, you can't. Array types are not suitable for use as map key types.

If you happen to get such a type to compile, then your implementation
happens to support a usage that is not part of standard C++, and there's no
way to know what it will do without knowing the details of your
implementation.
 
T

tony_in_da_uk

I need to be able to use map<char[256], T*>, I can make it
std::map<std::string, T*>, however, Key is passed by char[], hence I
would need to call c_str() all the time. What do you suggest?

I assume this is motivated by a concern that c_str() might be
inefficient, allocating some new storage with space for the extra
NUL. Don't worry about it - to the best of my knowledge, no STL past
or present does that. You can call c_str() and it will perform fine.
If your motivation is actually convenience, then consider that
std::string omits operator const char* for a good reason, and don't
fight decades of community experience. - Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top