473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

using map<char[256], T*>

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?
Sep 23 '08 #1
7 2697
puzzlecracker wrote:
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?
There is no comparison function for arrays. There is one for pointers
(and your arrays will be converted to pointers for that, unfortunately)
but that's not what you want, I reckon. Now, regarding your
requirements, what do you mean by "Key is passed by char[]"? Passed
where? By whom? Show us how you intend to use your map.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 23 '08 #2
On Tue, 23 Sep 2008 07:13:28 -0700, puzzlecracker wrote:
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?
Use std::string every where possible. If your pseudo type is
defined by 256 chars, then wrap it in a class definition.

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_in_English
Sep 23 '08 #3
puzzlecracker wrote:
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

Sep 23 '08 #4
On 2008-09-23 16:13, puzzlecracker wrote:
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&) { /* ... */ }
};

--
Erik Wikström
Sep 23 '08 #5
In message
<f9**********************************@j22g2000hsf. googlegroups.com>,
puzzlecracker <ir*********@gmail.comwrites
>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.
--
Richard Herring
Sep 24 '08 #6
"puzzlecracker" <ir*********@gmail.comwrote in message
news:f9**********************************@j22g2000 hsf.googlegroups.com...
>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.
Sep 24 '08 #7
On Sep 23, 11:13 pm, puzzlecracker <ironsel2...@gmail.comwrote:
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
Sep 25 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David | last post by:
Hello, I try to store PROLOG_TERMs in a map which is again stored in another map, to realise this I am using a composite key of two strings. The class compiles fine, but when I insert an element,...
6
by: me | last post by:
Hi guys - the question is in the subject line. I thought of one quick way: std::ifstream input("myfile.dat") ; std::istreambuf_iterator beg(input), end ; std::vector DataFile(beg,end) ;
8
by: Earl Purple | last post by:
On VC++.NET it is implemented like this static int __cdecl compare ( const _Elem *_First1, const _Elem *_First2, size_t _Count ) { // compare [_First1, _First1 + _Count) with [_First2, ...)...
13
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar,...
8
by: Marco Costa | last post by:
Hello all, I wrote a simple ODBC wrapper class that used code like this ( not real code, added types for clarification ): char** type bufs = new char* for( int i = 0 ; i < numberOfColumns ;...
1
by: ictheion | last post by:
Hi, I am having a problem utilizing ofstream*'s stored in a map (map<char, ofstream*>). I'm having basically the same problem with a map<char, boost::mutex*>. My code compiles, but at run-time I...
1
by: sharmadeep1980 | last post by:
Hi All, I am facing a very unique problem while compling my project in "Release" build. The project is building in DEBUG mode but giving linking error on Release build. Here is the error:...
4
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual...
4
jlm699
by: jlm699 | last post by:
I've looked at the other articles about maps of maps and am still stuck on this! I'm trying to basically make an enumeration of a data monitoring app. Instead of displaying numbers for certain...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.