Vector Iterator Problem

Having trouble iterating through a vector in a function:

1
2
3
4
5
6
7
int main()
{
   ...
   vector<int> customer;
   vector<int> switch_tolerance;
   ...
}


1
2
3
4
5
6
7
8
bool is_switching(unsigned int this_second, unsigned int this_customer, const vector<int>& customer, const vector<int>& switch_tolerance)
{
    int n;
    vector<int>::iterator it;

    it = find(customer.begin(), customer.end(), this_customer);
    ...
}


The compiler errors out on my find call. Anyone see what is wrong?

Thanks.
Remember to post the error next time!

But I can see that you're passing your vector by const ref :-)

you need to use a const_iterator

vector<int>::const_iterator it;

Andy

P.S. It would be better C++ style to use

vector<int>::const_iterator it = find(customer.begin(), customer.end(), this_customer);

In your case, the iterator's default constructor is called, and then its operator=

In the case I've just given, just the assignment constructor is triggered. (Class obj = val; and Class obj(val); are the same in this respect. The latter form is less readable in this case (for me) as the instance name is so much smaller than the find(..) expression.

Last edited on
Thanks. I made that change, but it still errors out. I didn't post the error because it's very long and cryptic (to me, at least). Here it is, in all its glory:

main.cpp|213|error: no matching function for call to 'find(__gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >, unsigned int&)'|


Maybe this will make sense to you? Thanks for your help!
Does casting this_customer to int fix the problem?

... = find(customer.begin(), customer.end(), (int)this_customer);

(if this works, should at least use a proper C++ cast)

If not, what standard headers are you including?
I tried that, but it didn't change anything. Here are all my headers:

1
2
3
4
5
6
7
#include <iostream>
#include <cstdlib>
#include <deque>
#include <vector>
#include <ctime>

using namespace std;


Thanks again. I'm really stumped.
closed account (DSLq5Di1)
Missing the algorithm header!
Thanks. As a C++ beginner, how would I know to look for this? I looked at the iterator reference page on this site, and I see no mention of needing the algorithm header. This is the kind of stuff that makes me crazy about learning C++, so I'm obviously trying to make this process less frustrating.

Any advice is appreciated. Thanks again for catching that.
<algorithm> is needed for std::find()

And all the other algorithic functions like find_if, sort, replace, remove, remove_if...

I use MSDN to check things stl related, but I'm a Windows programmer so I use that site as a matter of routine.

But sometimes I use SGI's site, as their stl was (is) one of the reference implementations: http://www.sgi.com/tech/stl/find.html

The error messages produced for template problems aren't as bad as they used to be, but they are rather arcane. But you soon learn to spot the key bits!
Thanks for all the help!
Topic archived. No new replies allowed.