472,325 Members | 1,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 software developers and data experts.

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 2716
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...
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...
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...
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...
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...
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...
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,...
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.