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: - #include <stdio.h>
-
#include <map>
-
-
class Test {
-
private:
-
class cmp {
-
private:
-
public:
-
long Ncomp;
-
cmp() {Ncomp = 0;};
-
cmp(long n) {Ncomp = n;};
-
bool operator() (const char* s1, const char* s2) {
-
return memcmp( s1, s2, Ncomp ) < 0;
-
};
-
};
-
std::map<char*,int,cmp> sols; // like this it uses cmp(),
-
// but how to make it use e.g. cmp(5) instead?
-
public:
-
void AddString(char*);
-
void ListStrings(void);
-
};
-
-
void Test::AddString(char*s) {
-
char*c;
-
c = new char[10]; // don't be bothered by the fixed length, it's just an example :)
-
memcpy(c,s,10);
-
sols[c]++;
-
}
-
-
void Test::ListStrings(void) {
-
std::map<char*,int,cmp>::iterator it;
-
for (it = sols.begin(); it != sols.end(); it++) {
-
printf("%s %d\n",it->first,it->second);
-
}
-
}
-
-
int main() {
-
Test t;
-
t.AddString("123");
-
t.AddString("123");
-
t.AddString("234");
-
t.ListStrings();
-
return 0;
-
}
-
Outside the class declaration (i.e. in a function declaration) one could use something like - 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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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{
|
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...
|
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...
|
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{
|
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...
|
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()
{
//
|
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...
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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: 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,...
| |