473,568 Members | 2,923 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please Help: problem using char[9] as key type of map

Dear all,

I am stuck on this problem, couldn't find an answer on any C++ book in
the bookshop.

Following is a cut of my program, and its error message:
=============== =============== ==============
280 map<char[9],vector<int> > _dict;
281 char* op_addr = op.get_address( );
282 int op_time = op.get_time_sta mp();
283 _dict[op_addr].push_back(op_t ime);
=============== =============== ============

class.cc:283: no match for `std::map<char[9], std::vector<int ,
std::allocator< int> >, std::less<char[9]>,
std::allocator< std::pair<const
char[9], std::vector<int , std::allocator< int> > > > >& [char*&]'
operator
/usr/include/c++/3.2.2/bits/stl_map.h:221: candidates are: _Tp&
std::map<_Key,
_Tp, _Compare, _Alloc>::operat or[](const _Key&) [with _Key =
char[9], _Tp =
std::vector<int , std::allocator< int> >, _Compare =
std::less<char[9]>,
_Alloc = std::allocator< std::pair<const char[9], std::vector<int ,
std::allocator< int> > > >]

=============== =============== ===============

I wrote a small test program, to represent this problem, using char[]
as key type for a map. But it always fails to compile. I really don't
know how to make this char array to work. The reason I have to use
char[] in stread of stl::string, because it's primitive type, save lots
of memory, and also can help the process to give memory back to OS.
(input file is about 6Gb)

#include <iostream>
#include <map>

int main()
{
typedef std::map<char[9],int > DictType;
DictType _dict;

char _addr[9] = "12345678";
_dict.insert( DictType::value _type(_addr, 3) );
std::cout<<_dic t[_addr]<<std::endl;

return 0;
}

It fails to compile, with following error message:

[root@localhost Trace_ana_stack _12May]# g++ -o try_char_map.ex e
try_char_map.cc
/usr/include/c++/3.2.2/bits/stl_pair.h: In constructor `std::pair<_T1,
_T2>::pair(cons t _T1&, const _T2&) [with _T1 = const char[9], _T2 =
int]':
try_char_map.cc :10: instantiated from here
/usr/include/c++/3.2.2/bits/stl_pair.h:84: ISO C++ forbids assignment
of arrays

=============== =============== ===========

I am really got no idea. Any help or comment is highly appreciate!

Many thanks!
Charlie

Jul 23 '05 #1
6 9852
Charlie wrote:
Dear all,

I am stuck on this problem, couldn't find an answer on any
C++ book in the bookshop.

Following is a cut of my program, and its error message:
=============== =============== ==============
280 map<char[9],vector<int> > _dict;


You can't use an array in a standard container.

You could use a std::string, or a std::vector<cha r>, or
boost::array<>. Or you could wrap your array:

struct char9
{
char ch[9];
bool operator<(char9 const &c);
};

Jul 23 '05 #2
Charlie wrote:
Dear all,

I am stuck on this problem, couldn't find an answer on any C++ book in
the bookshop.

Following is a cut of my program, and its error message:
=============== =============== ==============
280 map<char[9],vector<int> > _dict;
281 char* op_addr = op.get_address( );
282 int op_time = op.get_time_sta mp();
283 _dict[op_addr].push_back(op_t ime);
=============== =============== ============

class.cc:283: no match for `std::map<char[9], std::vector<int ,
std::allocator< int> >, std::less<char[9]>,
std::allocator< std::pair<const
char[9], std::vector<int , std::allocator< int> > > > >& [char*&]'
operator
/usr/include/c++/3.2.2/bits/stl_map.h:221: candidates are: _Tp&
std::map<_Key,
_Tp, _Compare, _Alloc>::operat or[](const _Key&) [with _Key =
char[9], _Tp =
std::vector<int , std::allocator< int> >, _Compare =
std::less<char[9]>,
_Alloc = std::allocator< std::pair<const char[9], std::vector<int ,
std::allocator< int> > > >]

=============== =============== ===============

I wrote a small test program, to represent this problem, using char[]
as key type for a map. But it always fails to compile. I really don't
know how to make this char array to work. The reason I have to use
char[] in stread of stl::string, because it's primitive type, save lots
of memory, and also can help the process to give memory back to OS.
(input file is about 6Gb)

#include <iostream>
#include <map>

int main()
{
typedef std::map<char[9],int > DictType;
DictType _dict;

char _addr[9] = "12345678";
_dict.insert( DictType::value _type(_addr, 3) );
std::cout<<_dic t[_addr]<<std::endl;

return 0;
}

It fails to compile, with following error message:

[root@localhost Trace_ana_stack _12May]# g++ -o try_char_map.ex e
try_char_map.cc
/usr/include/c++/3.2.2/bits/stl_pair.h: In constructor `std::pair<_T1,
_T2>::pair(cons t _T1&, const _T2&) [with _T1 = const char[9], _T2 =
int]':
try_char_map.cc :10: instantiated from here
/usr/include/c++/3.2.2/bits/stl_pair.h:84: ISO C++ forbids assignment
of arrays

=============== =============== ===========

I am really got no idea. Any help or comment is highly appreciate!

Many thanks!
Charlie


It'll be tough to get 6GB of data into a 'map' unless your
machine has a huge amount of RAM.

As 'Old Wolf' said, you can't use a char[] as a map key.
You can create a simple class to wrap you char[] so that
it can be used in a map or set. Below is a simple example
to get you started.

#include <iostream>
#include <cstring>
#include <map>

struct KEY9
{
char val[9];

KEY9()
{
std::memset(val , '\0', 9);
}

KEY9(const KEY9& oth)
{
std::memmove(va l, oth.val, 9);
}

KEY9(const char * str)
{
std::memset(val , '\0', 9);
if (str)
{
strncpy(val, str, 8);
}
}

// required for 'map', 'set', etc
bool operator<(const KEY9& oth) const
{
return std::strcmp(val , oth.val) < 0;
}
};

int main()
{
typedef std::map<KEY9, int> DictType;
DictType dict;

KEY9 addr1("12345678 ");
KEY9 addr2("hello");

dict.insert( DictType::value _type(addr1, 3) );
dict.insert( DictType::value _type(addr2, 4) );

std::cout << addr1.val << ": " << dict[addr1] << std::endl;
std::cout << addr2.val << ": " << dict[addr2] << std::endl;

return 0;
}

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #3
Dear Larry,

Thank you sooo much for kindly providing such a nice example! It helped
me a lot! With your example, I can now get this problem successfully
solved!

Best regards,
Charlie

Jul 23 '05 #4
Larry I Smith wrote:
struct KEY9
{
char val[9];
KEY9()
{
std::memset(val , '\0', 9);
}
KEY9(const KEY9& oth)
{
std::memmove(va l, oth.val, 9);
}


Good answer. Is there any reason for preferring these
functions to:

std::fill_n(val , 9, 0)
std::copy(oth.v al, oth.val + 9, val);

? BTW I think memcpy can be used rather than memmove, as val
and oth.val cannot overlap (you can't copy construct an object
from itself).

Jul 23 '05 #5
Old Wolf wrote:
Larry I Smith wrote:

struct KEY9
{
char val[9];
KEY9()
{
std::memset(val , '\0', 9);
}
KEY9(const KEY9& oth)
{
std::memmove(va l, oth.val, 9);
}

Good answer. Is there any reason for preferring these
functions to:

std::fill_n(val , 9, 0)
std::copy(oth.v al, oth.val + 9, val);

? BTW I think memcpy can be used rather than memmove, as val
and oth.val cannot overlap (you can't copy construct an object
from itself).


25 years of paranoid defensive programming, and performance
considerations. Often, the mem*() functions are implmented
in Assembly Language; this can be a major performance
consideration if the OP plans to use many KEY9 objects
(thousands in a container perhaps). Yes memcpy() is OK
in the constructors, but I've been conditioned by my
company's 'technical standards' to always use memmove().

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #6
Old Wolf wrote:
Larry I Smith wrote:

struct KEY9
{
char val[9];
KEY9()
{
std::memset(val , '\0', 9);
}
KEY9(const KEY9& oth)
{
std::memmove(va l, oth.val, 9);
}

Good answer. Is there any reason for preferring these
functions to:

std::fill_n(val , 9, 0)
std::copy(oth.v al, oth.val + 9, val);

Yes, it's much more likely that memset and related functions
are specialized for char then standard algorithms.

Jul 23 '05 #7

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

Similar topics

3
2968
by: Mahmood Ahmad | last post by:
Hello, I have written a program that reads three types of records, validates them acording to certain requirements and writes the valid records into a binary file. The invalid records are supposed to be reported on the printer but I have commented those pieces of code and have got those records printed on the screen. I am using Microsoft...
7
2372
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help me. This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text. I have done Exercise 7 for you: Below you will find the...
5
2518
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now when my function returns, the values stored in the char* are some garbage values (perhaps because I didn't allocate any memory for them).. but even if...
4
8027
by: Carl Harris | last post by:
I am trying to write some code to: 1.Prompt a user for filenames 2.Open the files 3.Convert my plain text into a cipher text array/string bear in mind I am a novice! I have wriiten some code already which completes takes 1 and 2 but haven't got a clue with the conversion (task 3)
15
2727
by: Buck Rogers | last post by:
Hi guys! Task 1: Write a program which presents a menu with 5 options. The 5th option quits the program. Each option should execute a system command using system(). Below is my humble offering. =================================================== #include <stdio.h> #include <stdlib.h>
11
1506
by: Steve Clay | last post by:
I have a small C program for a college course. It is meant to encrypt and decrypt lower case letters and leave spaces as spaces. I can't get it to run properly as I think I have a problem in the While construct area. It is driving me mad trying to sort it out - can someone please help. The program is: /* * Program to encrypt a...
2
2043
by: Simon | last post by:
platform: Borland C++ 5.5 free Winxp C++ source from: C++ primer Plus 5 Edition. Complie Error: "operator >>" not implemented in type 'istream' for arguments of type "STRING" in function main() //mystring.h #ifndef MYSTRING_H_
3
1846
by: Alami | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h>...
2
5303
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I managed to write a code for a single player (human -vs- computer) game. My problem is, now we want to merge the two games with options to pick which...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.