473,473 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Initialise list in STL

sam
Hi,

Can anyone tell me how to initialise list<HashMap> in STL?
I written the following function, but if the "key" is not in the hash
table, the return value causes:
"terminate called after throwing an instance of 'std::length_error'
what(): vector::reserve
Abort (core dumped)
"

The function I written is shown as follow:

HashMap Parser::find_rule(string key_name, string i)
{
list<HashMap>::iterator l_iter;
HashMap::iterator m_iter;
for (l_iter=l_rule.begin(); l_iter!=l_rule.end(); l_iter++) {
for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
if (m_iter->first == key_name) {
if (i != "") {
if (m_iter->second == i)
return *l_iter;
}
else
return *l_iter;
}
}
}
return *l_iter;
}

Thanks
Sam
Jul 23 '05 #1
2 2793
sam
sam wrote:
Hi,

Can anyone tell me how to initialise list<HashMap> in STL?
I written the following function, but if the "key" is not in the hash
table, the return value causes:
"terminate called after throwing an instance of 'std::length_error'
what(): vector::reserve
Abort (core dumped)
"

The function I written is shown as follow:

HashMap Parser::find_rule(string key_name, string i)
{
list<HashMap>::iterator l_iter;
HashMap::iterator m_iter;
for (l_iter=l_rule.begin(); l_iter!=l_rule.end(); l_iter++) {
for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
if (m_iter->first == key_name) {
if (i != "") {
if (m_iter->second == i)
return *l_iter;
}
else
return *l_iter;
}
}
}
return *l_iter;
}
What want to do is I want to assign an empty hashmap as an return value
if the key is not found in the search. Can anyone please tell me how to
initialise an empty hashmap or change the above looping for a better
approach in seaching for alist of hashmap?
Thanks
Sam

Jul 23 '05 #2
On Fri, 29 Apr 2005 14:04:25 +0800, sam <sam++@--.com> wrote:
sam wrote:
Hi,

Can anyone tell me how to initialise list<HashMap> in STL?
HashMap is not part of the STL. Does it support operator[] and find
as map does?
I written the following function, but if the "key" is not in the hash
table, the return value causes:
"terminate called after throwing an instance of 'std::length_error'
what(): vector::reserve
Abort (core dumped)
"

The function I written is shown as follow:

HashMap Parser::find_rule(string key_name, string i) You probably mean to return HashMap&
{
list<HashMap>::iterator l_iter;
HashMap::iterator m_iter;
for (l_iter=l_rule.begin(); l_iter!=l_rule.end(); l_iter++) {
for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
if (m_iter->first == key_name) { /*****/ if (i != "") {
if (m_iter->second == i)
return *l_iter;
}
else
return *l_iter; You can reduce the statements between '/*****/' and here to:
if (i == "" || i == m_iter->second)
return *l_iter; }
}
}
return *l_iter; Instead of returning l_iter (which now == l_rule.end()), add a HashMap
to l_rule:
l_rule.push_back(HashMap());
/* add a blank entry for key_name */
//l_rule.back()[key_name] = "";
/* add an entry for key_name with value i */
//l_rule.back()[key_name] = i;
return l_rule.back(); }

What want to do is I want to assign an empty hashmap as an return value
if the key is not found in the search. Can anyone please tell me how to
initialise an empty hashmap or change the above looping for a better
approach in seaching for alist of hashmap?

We can't say for certain how to create an empty HashMap because we
don't know how HashMap is defined. If its default constructor creates
an empty HashMap, then the default constructor provides two
approaches: create a temporary using the syntax 'HashMap()' and define
a local with no arguments for the constructor and without assignment
(e.g. 'HashMap tmp;').

If HashMap has a find method with the same semantics as map::find,
try:

HashMap& Parser::find_rule(string key_name, string i)
{
list<HashMap>::iterator l_iter;
HashMap::iterator m_iter;
for (l_iter=l_rule.begin(); l_iter!=l_rule.end(); l_iter++) {
if ((m_iter = l_iter->find(key_name) != l_iter->end()) {
if (i == "" || i == m_iter->second)
return *l_iter;
}
}
l_rule.push_back(HashMap());
/* add a blank entry for key_name */
l_rule.back()[key_name] = "";
/* add an entry for key_name with value i */
//l_rule.back()[key_name] = i;
return l_rule.back();
}

Kanenas
Jul 23 '05 #3

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

Similar topics

12
by: Yu | last post by:
I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can...
9
by: iceColdFire | last post by:
HI, I have a function as void f(int p) { return p++; } now I have created a function pointer as
6
by: Stuart Norris | last post by:
Dear Readers, I am attempting to initialise a struct contiaing a dynamic character string. In the example below I am trying to initialise the name field so that my struct does not waste space. ...
4
by: steve | last post by:
I want to set all elements in a numeric array to a value other than 0. The default numeric initialisers set the elements to 0. Is looped assignment the fastest way, or is there an equivalent to the...
3
by: Annie | last post by:
hello all, is it possible at all? i have a class that encapsulates a Hashtable collection. What i want is to initialise this object once and only once and then be able to access its Public...
1
by: zoneal | last post by:
I have two textbox server control. 1. Registration Date 2. Expiry Date. Expiry Date is calculated automatically from table on the basis of scheme which was selected by user from Scheme...
2
by: Charles Law | last post by:
Does anyone know if it is possible to initialise a structure array at run-time, something like this: <code> Structure struct Dim a As String Dim b As Integer Dim c As End Structure
2
by: Tomás | last post by:
Up until lately I've been writing all mad kinds of code for accomplishing things, but lately I've decided to lean more toward the whole readability, etc. side of things. I have a command line...
14
by: Frederick Gotham | last post by:
How do we initialise an aggregate member object of a class? The following won't compile for me: struct MyStruct { int i; double d; }; class MyClass { private:
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.