C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Specifying Initial bucket size in hash_map

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
deepak.sethi@gmail.com
Guest





PostPosted: Tue Aug 16, 2005 2:35 pm    Post subject: Specifying Initial bucket size in hash_map Reply with quote



I am interested in specifying the Initial bucket size for hash_map
declared in the class.
There is no member function to do so except the constructor.

For eg
Class A
{
hash_map <int,string> h;
}
The default size is 100 and i want to create "h" with a initial size
1000.

I cannot use
"hash_map <int,string> h(100);" in the .h file as this is static
initialization.

Do i have to use the Constructor of class A or is there any other way.
Kindly provide psuedo-code


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
mlimber
Guest





PostPosted: Wed Aug 17, 2005 8:53 am    Post subject: Re: Specifying Initial bucket size in hash_map Reply with quote



[email]deepak.sethi (AT) gmail (DOT) com[/email] wrote:
Quote:
I am interested in specifying the Initial bucket size for hash_map
declared in the class.
There is no member function to do so except the constructor.

For eg
Class A
{
hash_map <int,string> h;
}
The default size is 100 and i want to create "h" with a initial size
1000.

I cannot use
"hash_map <int,string> h(100);" in the .h file as this is static
initialization.

Do i have to use the Constructor of class A or is there any other way.
Kindly provide psuedo-code

You need to use the constructor to initialize members. You can use the
member initialization list like this:

class A
{
public:
A( size_t bucketSize = 1000 )
: h( bucketSize )
{}

private:
hash_map <int,string> h;
}

Cheers! --M


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
jrm
Guest





PostPosted: Wed Aug 17, 2005 8:55 am    Post subject: Re: Specifying Initial bucket size in hash_map Reply with quote



You can initialize h in A's ctor:

class A
{
public:


A() :
h(1000)
{
}


size_t foo() const { return h.bucket_count(); }


private:

hash_map<int, string> h;
};


int main()
{
A a;


cout<

return 0;


}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ali Çehreli
Guest





PostPosted: Thu Aug 18, 2005 1:06 pm    Post subject: Re: Specifying Initial bucket size in hash_map Reply with quote

<deepak.sethi (AT) gmail (DOT) com> wrote

Quote:
I am interested in specifying the Initial bucket size for hash_map
declared in the class.
There is no member function to do so except the constructor.

That means that you have to provide the bucket size at construction time.

Quote:
For eg
Class A
{
hash_map <int,string> h;
}
The default size is 100 and i want to create "h" with a initial size
1000.

I cannot use
"hash_map <int,string> h(100);" in the .h file as this is static
initialization.

Do i have to use the Constructor of class A or is there any other way.

Since h is constructed during the construction of A, and you have to provide
the size at that time, that would be the best time :)

A::A()
:
h(100)
{}

Another method would be to use a member function to initialize h:

void A::initialize_h(int size)
{
h = hash_map<int, string>(size);
}

Since the above clears the contents of h, you may want to copy the contents
to a temporary hash_map of size 'size' before doing the assignment. Also
renaming the function to 'resize_h':

void A::resize_h(int size)
{
hash_map<int, string> new_h(size);

// copy the old contents to new_h here
// (Your hash_map may have a constructor to achieve this.)

h = new_h;
}

Ali


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.