473,785 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ 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_er ror'
what(): vector::reserve
Abort (core dumped)
"

The function I written is shown as follow:

HashMap Parser::find_ru le(string key_name, string i)
{
list<HashMap>:: iterator l_iter;
HashMap::iterat or 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 2808
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_er ror'
what(): vector::reserve
Abort (core dumped)
"

The function I written is shown as follow:

HashMap Parser::find_ru le(string key_name, string i)
{
list<HashMap>:: iterator l_iter;
HashMap::iterat or 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_er ror'
what(): vector::reserve
Abort (core dumped)
"

The function I written is shown as follow:

HashMap Parser::find_ru le(string key_name, string i) You probably mean to return HashMap&
{
list<HashMap>:: iterator l_iter;
HashMap::iterat or 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_bac k(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_ru le(string key_name, string i)
{
list<HashMap>:: iterator l_iter;
HashMap::iterat or m_iter;
for (l_iter=l_rule. begin(); l_iter!=l_rule. end(); l_iter++) {
if ((m_iter = l_iter->find(key_nam e) != l_iter->end()) {
if (i == "" || i == m_iter->second)
return *l_iter;
}
}
l_rule.push_bac k(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
5482
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 initialize the static object without invoking the constructor? Below is the sample coding. Header file ASURegistrationManager.h #include "ASURegistration.h"
9
2526
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
5436
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. I know if I change char name this will work, but I will waste alot of space. (I am learning so I want to learn the best way) How can I define a struct the allows variable length character strings in the
4
2102
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 C RTL memset. Cheers Steve
3
1097
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 Static methods always. What i want is when the user logs in i gets its detail and add him/her in the hashtable until her/she is online and similarly
1
954
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 DropDown ListBox control. eg.
2
7212
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
1765
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 application, which I intend to be used like so: unicon.exe -8to32 source.txt target.txt There are three command line arguments. The first is something like:
14
1830
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
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.