problem with iterators

Started by
6 comments, last by Zahlman 17 years, 4 months ago
hello all i'm trying to run a function of a class on every elemt in a vector. for some reason the program just crashes (i dont get any erros while compiling it) and i have no idea what is the problem. anyways here is the code:

vector<objectsinfo *>::iterator da;
da = (objinfo.end());
vector<int>::iterator z;

for (z = tempthing.begin(); z !=tempthing.end(); z++) 
{
    (*da)->addoption((*z));
}

anyone has any idea what can cause this and how to fix this? thanks in advance
Advertisement
You're dereferencing da, which is a past-the-end iterator by the looks of it.
sorry to waste a post for this, but just wanted to memind you that you should not be using a post-increment operator anywhere you don't have to, like EVER in a for loop. The problem is that this operator cannot possibly be implemented efficiently in custom written classes, so it makes all your code which uses it not near as object-oriented / maintainable as it would otherwise be (because if you decide to switch from std::vector::iterator to say binary_tree::bredth_first_iterator ... then your going to be creating copies of the iterator needlessly - which in the case of many forms of iteration is not cost-free. always use the standard increment operator (unfortunately called "preincrement" to distinguish it from "postincrement" ... but incrementing is normally "pre" ... 3 of these 4 things are equal ... 1 is not ...

x = x + 1;
x += 1;
++x;
x++;

as you can see, there is the general purpose add and store, the built-in add to operator, the built-in increment operator (pre), and the very special purpose and most incorectly used postincrement operator.

The only reason this is not a major efficiency bug is because for primative types optimizing compilers make such wastefull postincrements emit the same code as a preincrement ... but such is often not possible for more advanced custom types.

Once again, sorry for hijacking this thread for my off-topic rant. I guess I should just take this code and make a new thread ... "Newbies, postincrement considered harmfull" or some such.
Quote:Original post by mc30900
hello all i'm trying to run a function of a class
on every elemt in a vector.
for some reason the program just crashes (i dont get any erros while compiling it)
and i have no idea what is the problem.
anyways here is the code:
*** Source Snippet Removed ***
anyone has any idea what can cause this and how to fix this?

thanks in advance


Hi,

I had a bit of trouble trying to understand your code, but from what I've gathered, std::for_each might help you out:


class foo {
public:
void func() { }
};

int main()
{
// Create a vector of 5 foo objects
vector bar(5);

// For every element in the vector, call func() from the foo class
for_each(bar.begin(), bar.end(), mem_fun_ref(&foo::func));
}

for_each is in the standard algorithm header, mem_fun_ref can be found in the functional header.

HTH.
end() does not give you an iterator pointing to the last item, it is PAST the last item! Use rbegin() to get an iterator to the last item.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
well, as ppl suggested i changed end to rbegin.
now i have an error when i'm trying to compile this.
here is the code:
vector <objectsinfo *>::iterator da;da = objinfo.rbegin();

the error is on the second line:
327 C:\main.cpp no match for 'operator=' in 'da = std::vector<_Tp, _Alloc>::rbegin() [with _Tp = objectsinfo*, _Alloc = std::allocator<objectsinfo*>]()' 


anyone has any idea what can cause this?
thanks in advance
Quote:Original post by mc30900
well, as ppl suggested i changed end to rbegin.
now i have an error when i'm trying to compile this.
here is the code:
*** Source Snippet Removed ***
the error is on the second line:
*** Source Snippet Removed ***

anyone has any idea what can cause this?
thanks in advance
rbegin() returns an object of type reverse_iterator, which you cannot assign to an object of type iterator.

If you just want to access the last element in the container, use the function back(). Revising your original example accordingly:
for (z = temp_thing.begin(); z !=temp_thing.end(); ++z) {    obj_info.back().add_option(*z);}
In addition to the suggestions made in the previous posts, I'll point out that you're using unnecessary parentheses in a few places, and that you should use underscores in your names if you're going to use all lower-case.
Just to be clear, you want to add *all* the options to the *last* object-info? (I hope you have better variable names in your real code x.x)

This topic is closed to new replies.

Advertisement