473,396 Members | 1,933 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,396 software developers and data experts.

Using STL maps with custom constructor in a class

Hi,

I've searched many groups and discussions, but nothing I found suited my needs. My problem is rather simple: I want to use a STL map in a class with a custom constructor but don't know how to tell it to use my constructor instead of the default.

The following code illustrates my problem:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <map>
  3.  
  4. class Test {
  5.     private:
  6.         class cmp {
  7.             private:
  8.             public:
  9.                 long Ncomp;
  10.                 cmp()       {Ncomp = 0;};
  11.                 cmp(long n) {Ncomp = n;};
  12.                 bool operator() (const char* s1, const char* s2) {
  13.                     return memcmp( s1, s2, Ncomp ) < 0;
  14.                 };
  15.         };
  16.         std::map<char*,int,cmp> sols; // like this it uses cmp(),
  17.         // but how to make it use e.g. cmp(5) instead?
  18.     public:
  19.         void AddString(char*);
  20.         void ListStrings(void);
  21. };
  22.  
  23. void Test::AddString(char*s) {
  24.     char*c;
  25.     c = new char[10]; // don't be bothered by the fixed length, it's just an example :)
  26.     memcpy(c,s,10);
  27.     sols[c]++;
  28. }
  29.  
  30. void Test::ListStrings(void) {
  31.     std::map<char*,int,cmp>::iterator it;
  32.     for (it = sols.begin(); it != sols.end(); it++) {
  33.         printf("%s %d\n",it->first,it->second);
  34.     }
  35. }
  36.  
  37. int main() {
  38.     Test t;
  39.     t.AddString("123");
  40.     t.AddString("123");
  41.     t.AddString("234");
  42.     t.ListStrings();
  43.     return 0;
  44. }
  45.  
Outside the class declaration (i.e. in a function declaration) one could use something like
Expand|Select|Wrap|Line Numbers
  1. std::map<char*,int,cmp> sols2((5));
but this doesn't work in the declaration, of course.

Hope someone knows better than me,
regards,
Alexander
May 3 '07 #1
1 2241
Hi,
There is no way to associate a particular ctor to a container.
if you take a look at std::map help, the third arg is a comparison function
which helps the container in sorting, it has nothing to with object creation.

One more thing, When you use the map's[] operator , a default object is created(if not already present) and assigned to the key that you used as index, the assignment that you do for this key is calling a copy ctor.

But map also has insert method which takes pair<Key, obj> , you can also use this method to populate the conatiner.Here since you are going to create the object at first place you can use the ctor that u want. Insert won't always insert if the key is already present.Please look at the map's doc for deatils .

Regards
Prasannaa.
May 8 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Frank Wisniewski | last post by:
I have the following persudo code: //My Form Class class Form1 { //Local Variable for my custom object private MyCustomObject1 //Constructor for Form Class public Constructor{
3
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a...
9
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it...
3
by: Frank Wisniewski | last post by:
I have the following persudo code: //My Form Class class Form1 { //Local Variable for my custom object private MyCustomObject1 //Constructor for Form Class public Constructor{
3
by: ExclusiveResorts | last post by:
Can the CallContext be used reliably for storing request specific data? We are developing an application library that uses the CallContext to keep an IdentityMap (hashtable of business objects...
3
by: hharry | last post by:
hello all, quick syntax question. i am writing a custom exception class and came across an example: public class dataProvException : ApplicationException { public dataProvException() { //
2
by: Tugrul HELVACI | last post by:
I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String; fPersonSurName : String; fPersonAge : Integer; published property PersonName : String...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
2
by: gilbert | last post by:
Hello. I am trying to use c# generic to define a class. class MyClass<Twhere T: new(){ } In this definition, MyClass can create T objects with a default constructor. Is there any way to...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.