Hi there!
I'm having a little (really not so little) issue using iterators.

// object t1 created here.
std::vector<std::string> *f1_t1 = t1.func1();
std::vector<std::string>::iterator iterv;

for (iterv = f1_t1->begin(); iterv != f1_t1->end(); ++iterv)
    std::cout << *iterv << " ";
std::cout << std::endl;

std::vector<std::string> *f2_t1 = t1.func2();

for (iterv = f2_t1->begin(); iterv != f2_t1->end(); ++iterv)
    std::cout << *iterv << " ";
std::cout << std::endl;

// object t2 created here
std::vector<std::string> *f1_t2 = t2.f1();

for (iterv = f1_t2->begin(); iterv != f1_t2->end(); ++iterv)
    std::cout << *iterv << " ";
std::cout << std::endl;

std::vector<std::string> *f2_t2 = t2.f2();

for (iterv = f2_t2->begin(); iterv != f2_t2->end(); ++iterv)
    std::cout << *iterv << " ";
std::cout << std::endl;

As you can see, I have two member functions that return two different vectors of strings. The vectors are returned as references and assigned to a pointer of the same type (vector of strings). Then, using an iterator, the content of the vectors is printed out.
In the first object, everything goes fine, but when the runtime go ahead to print the content of the vectors of the second object, it append the output that should be given to the output of the previous object. I know, is not clear, maybe and example could clarify.

Object one output:
Object two output:
output11 output21
output12 output22

As you can see, the outputs of the second object have the ones from the first in front of them.

Could someone help me to solve this issue?

Note: this piece of code is used to print out the content of a binary tree, if this information can be useful.

I don't see anything wrong with this code. My guess is that it is not a problem with either the printing or the iterators "resetting". It's most likely a problem with the creation of those vectors. In other words, what is being printed is exactly what is in those vectors. If you get the wrong things printed out, you probably put the wrong things into those vectors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.