copy of list iterator

S

sam

Hi,

This is a "list iterator" problem I expect it will copy the list
iterator (l_iter) to the caller:
eg.
list<HashMap>::iterator AcctConfParser::find_Acct_rule(string i)
{
list<HashMap>::iterator l_iter;
HashMap::iterator m_iter;
for (l_iter=macro_list.begin(); l_iter!=macro_list.end(); l_iter++) {
for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
if (m_iter->first == "index") {
if (m_iter->second == i)
return l_iter;
}
}
}
}

This function just return a reference to the caller, how can I make
modification so that the function will make a copy of the l_iter to the
caller?

Thanks
Sam
 
R

Rolf Magnus

sam said:
Hi,

This is a "list iterator" problem I expect it will copy the list
iterator (l_iter) to the caller:
eg.
list<HashMap>::iterator AcctConfParser::find_Acct_rule(string i)
{
list<HashMap>::iterator l_iter;
HashMap::iterator m_iter;
for (l_iter=macro_list.begin(); l_iter!=macro_list.end(); l_iter++) {
for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
if (m_iter->first == "index") {
if (m_iter->second == i)
return l_iter;
}
}
}
}

This function just return a reference to the caller,

Why would it? I don't see any reference in your code at all.
how can I make modification so that the function will make a copy of the
l_iter to the caller?

It already does. What makes you believe it doesn't?
 
H

Howard

sam said:
Hi,

This is a "list iterator" problem I expect it will copy the list iterator
(l_iter) to the caller:
eg.
list<HashMap>::iterator AcctConfParser::find_Acct_rule(string i)
{
list<HashMap>::iterator l_iter;
HashMap::iterator m_iter;
for (l_iter=macro_list.begin(); l_iter!=macro_list.end(); l_iter++) {
for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
if (m_iter->first == "index") {
if (m_iter->second == i)
return l_iter;
}
}
}
}

This function just return a reference to the caller, how can I make
modification so that the function will make a copy of the l_iter to the
caller?

It already *does* return a copy. It's return type is an iterator, not a
reference to one, and the return statement says to return a local variable's
value, which means to copy that value and return it.

But... from what I see, if the "find" fails, you're missing a return
statement at the end there. You need to return something in the case where
it's not found (or else throw an exception, I suppose).

-Howard
 
S

sam

Rolf said:
sam wrote:




Why would it? I don't see any reference in your code at all.




It already does. What makes you believe it doesn't?
The l_iter-> is a pointer, iterator is a type of pointer?

Sam.
 
C

Christian Meier

1. Iterators are usually implemented as pointers. But they do not have to.
2. Do not use postfix increment operator for objects if it is not necessary.
3. Your function must have a return statement for each case. Even if your
list is empty.

-Chris
 
R

Rolf Magnus

Please don't top-post.

Christian said:
1. Iterators are usually implemented as pointers.

No, they aren't. The only iterators that can be implemented as pointers are
those for std::vector, and even for those, it varies. In g++, e.g., they
aren't.
 
R

Rolf Magnus

sam said:
The l_iter-> is a pointer,

l_iter is an iterator, not a pointer. And a pointer is not a reference.
iterator is a type of pointer?

No. It just shares some of its behavior with that of a pointer. Anyway, I'm
not sure what you want now. Do you want to return l_iter? Or do you want to
return a reference or a pointer to the element of your container that it's
associated with? Or do you want to make a copy of that element and return
that?
Currently, you're returning a copy of l_iter (i.e. the iterator itself).
 
S

sam

Rolf said:
sam wrote:




l_iter is an iterator, not a pointer. And a pointer is not a reference.




No. It just shares some of its behavior with that of a pointer. Anyway, I'm
not sure what you want now. Do you want to return l_iter? Or do you want to
return a reference or a pointer to the element of your container that it's
associated with? Or do you want to make a copy of that element and return
that?
At the moment, I have to return *l_iter to HashMap::iterator, otherwise
I will get garbage from the returning item. I may be need to re-test it
again and see if this is really the case or caused by something else.

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top