473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

list traversal.

sam
Hi,

Can anyone please tell me how to simplify the following list traversal code?

for (l_iter=l_peer_info.begin(); l_iter!=l_peer_info.end(); l_iter++) {
hm = &(*l_iter);
m_iter=l_iter->begin();
while (m_iter!=l_iter->end()) {
if ((*l_iter)["key"] == "value") {
l_iter = l_peer_info.erase(l_iter);
l_iter--;
break;
}
else
m_iter++;

if (m_iter->first == "index")
i = m_iter->second;
else if (m_iter->first == "rule")
rule = m_iter->second;
else if (m_iter->first == "rule2")
rule2 = m_iter->second;
else if (m_iter->first == "method")
method = m_iter->second;
else if (m_iter->first == "rule3")
rule3 = m_iter->second;
else if (m_iter->first == "rule4")
rule4 = m_iter->second;
else if (m_iter->first == "rule5")
rule5 = m_iter->second;
......
}
}

Thanks
Sam
Jul 23 '05 #1
4 1621
* sam:

Can anyone please tell me how to simplify the following list traversal code?
The first step would be to get correct, working code.

for (l_iter=l_peer_info.begin(); l_iter!=l_peer_info.end(); l_iter++) {
hm = &(*l_iter);
Not used.
m_iter=l_iter->begin();
while (m_iter!=l_iter->end()) {
if ((*l_iter)["key"] == "value") {
l_iter = l_peer_info.erase(l_iter);
l_iter--;
Probably incorrect (think about erasing first element), or at least relying
on dubious assumptions.

break;
}
else
m_iter++;

if (m_iter->first == "index")
i = m_iter->second;
else if (m_iter->first == "rule")
rule = m_iter->second;
else if (m_iter->first == "rule2")
rule2 = m_iter->second;
else if (m_iter->first == "method")
method = m_iter->second;
else if (m_iter->first == "rule3")
rule3 = m_iter->second;
else if (m_iter->first == "rule4")
rule4 = m_iter->second;
else if (m_iter->first == "rule5")
rule5 = m_iter->second;
Put property-access-by-name functionality into the object being updated.

......
}
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
if l_peer_info is list<map<a,b> > then
it is possible to simplify this code:
make list<map<a,b> > as list<map<a,pair<b,c> > >, where is c is pointer
to desirable varieble, for example:
for "index" - &i, for "rule" - &rule, etc.
for "key" - NULL

for (l_iter=l_peer_info.begin(); l_iter!=l_peer_info.end(); l_iter++)
{
m_iter=l_iter->begin();
while (m_iter!=l_iter->end()) // for each
{
if ( m_iter->second.second == NULL )
{
l_iter = l_peer_info.erase(l_iter); // don't need to do l_iter--!!!
break;
}
*m_iter->second.second = m_iter->second.first;
m_iter++;
}
}

Jul 23 '05 #3
sam wrote:
Hi,

Can anyone please tell me how to simplify the following list traversal
code?

for (l_iter=l_peer_info.begin(); l_iter!=l_peer_info.end(); l_iter++) {
hm = &(*l_iter);
m_iter=l_iter->begin();
while (m_iter!=l_iter->end()) {
if ((*l_iter)["key"] == "value") { The line above indicates that your container is not a list,
but a map.
l_iter = l_peer_info.erase(l_iter);
l_iter--;
break;
}
else
m_iter++;

if (m_iter->first == "index")
i = m_iter->second;
else if (m_iter->first == "rule")
rule = m_iter->second;
else if (m_iter->first == "rule2")
rule2 = m_iter->second;
else if (m_iter->first == "method")
method = m_iter->second;
else if (m_iter->first == "rule3")
rule3 = m_iter->second;
else if (m_iter->first == "rule4")
rule4 = m_iter->second;
else if (m_iter->first == "rule5")
rule5 = m_iter->second;
......
}
}

Instead of having an if-then-else-if ladder,
perhaps you might want a container of <key, value>
pairs, where the key is the string and the value
is a pointer the a "rule" variable. A map seems
very suitable:
#include <map>
#include <string>

using std::map;
using std::string;

typedef map<string, string *> Rule_Container;

/* ... */
Rule_Container::const_iterator rule_iter;
rule_iter = rule_map.find(m_iter->first);
if (rule_iter != rule_map.end())
{
string * rule_variable_pointer;
rule_variable_pointer = rule_iter->second;
if (rule_variable_pointer)
{
*rule_variable_pointer = m_iter->second;
}
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #4
sam
Thomas Matthews wrote:
sam wrote:
Hi,

Can anyone please tell me how to simplify the following list traversal
code?

for (l_iter=l_peer_info.begin(); l_iter!=l_peer_info.end(); l_iter++) {
hm = &(*l_iter);
m_iter=l_iter->begin();
while (m_iter!=l_iter->end()) {
if ((*l_iter)["key"] == "value") {


The line above indicates that your container is not a list,
but a map.
l_iter = l_peer_info.erase(l_iter);
l_iter--;
break;
}
else
m_iter++;

if (m_iter->first == "index")
i = m_iter->second;
else if (m_iter->first == "rule")
rule = m_iter->second;
else if (m_iter->first == "rule2")
rule2 = m_iter->second;
else if (m_iter->first == "method")
method = m_iter->second;
else if (m_iter->first == "rule3")
rule3 = m_iter->second;
else if (m_iter->first == "rule4")
rule4 = m_iter->second;
else if (m_iter->first == "rule5")
rule5 = m_iter->second;
......
}
}


Instead of having an if-then-else-if ladder,
perhaps you might want a container of <key, value>
pairs, where the key is the string and the value
is a pointer the a "rule" variable. A map seems
very suitable:
#include <map>
#include <string>

using std::map;
using std::string;

typedef map<string, string *> Rule_Container;

/* ... */
Rule_Container::const_iterator rule_iter;
rule_iter = rule_map.find(m_iter->first);
if (rule_iter != rule_map.end())
{
string * rule_variable_pointer;
rule_variable_pointer = rule_iter->second;
if (rule_variable_pointer)
{
*rule_variable_pointer = m_iter->second;
}
}


I used hash_map. What is the primary difference with map?

Thanks
Sam
Jul 23 '05 #5

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

Similar topics

1
by: guy001 | last post by:
Hi, I'm trying to traverse the DOM in a bit of a non-traditional manner and am struggling to get my head around it. Just say i have some elements like so: A |-B |-C | |-D |
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
4
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows...
4
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
19
by: jason_box | last post by:
I'm alittle new at C and I'm trying to write a simple program that will record the frequency of words and just print it out. It is suppose to take stdin and I heard it's only a few lines but I'm...
16
by: Frank Neuhaus | last post by:
Hi I have some std list, I'd like to traverse. During the traversal, I want to conditionally delete some objects. My code for that is like this right now: for (std::list<myStruct>::iterator...
2
by: sieg1974 | last post by:
Hi, I have a linked list with 705 nodes, and the functions getContact and listContact to deal with it. listContact works properly, and prints all some debug information about each node. On the...
2
by: zoro | last post by:
hello: i'm not sure what the operations that would be affected if we didn't maintain a tail pointer in doubly linked list? so why is it important? your help is appreciated thank you
4
by: dineshv | last post by:
I want to see if there is an alternative method for fast list traversal. The code is very simple: dict_long_lists = defaultdict(list) for long_list in dict_long_lists.itervalues() for element...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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 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.