list traversal.

S

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
 
A

Alf P. Steinbach

* 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

alexey.br

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++;
}
}
 
T

Thomas Matthews

sam said:
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
 
S

sam

Thomas said:
sam said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top