Can find() apply on vector

L

lovecreatesbea...

The find() in <algorithm> requires two input iterators as its first and
second arguments. In some of my code, I use vector<T>::begin() and
vector<T>::end() as the first and second arguments. The T::eek:perator==
and T::eek:perator!= are defined. And the code works.

The begin() and end() of vector<T> are not input iterators but random
access iterators, right? Why I use find() on vectors, are there
something wrong?

Class OgFile{
friend operator == ();
friend operator != ();
//...
};

vector <OgFile> vecOgFile;
OgFile OgFile1;
//...

find (vecOgFile.begin(), vecOgFile.end(), OgFile1);
 
S

Salt_Peter

The find() in <algorithm> requires two input iterators as its first and
second arguments. In some of my code, I use vector<T>::begin() and
vector<T>::end() as the first and second arguments. The T::eek:perator==
and T::eek:perator!= are defined. And the code works.

If you say so.
The begin() and end() of vector<T> are not input iterators but random
access iterators, right? Why I use find() on vectors, are there
something wrong?

Who knows?
Class OgFile{
friend operator == ();
friend operator != ();
//...
};

vector <OgFile> vecOgFile;
OgFile OgFile1;
//...

find (vecOgFile.begin(), vecOgFile.end(), OgFile1);

Random access iterators will substitute for input iterators nicely.
Keep in mind that if std::find does not locate a match, it will return
one past the end of the container. What you don't want to do, is have
the program then access that fictitious element, you'll get a
segmentation fault. Thats by design.

#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
std::vector< std::string > vs;
vs.push_back( "string 0" );
vs.push_back( "string 1" );
vs.push_back( "string 2" );

typedef std::vector< std::string >::iterator VIter;
VIter viter = std::find(vs.begin(), vs.end(), "string 3");
// std::cout << "found: " << *viter << std::endl; // error
if (viter != vs.end())
{
std::cout << "found: " << *viter << std::endl;
} else {
std::cout << "not found." << std::endl;
}
}

Is that what your question was?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

The find() in <algorithm> requires two input iterators as its first and
second arguments. In some of my code, I use vector<T>::begin() and
vector<T>::end() as the first and second arguments. The T::eek:perator==
and T::eek:perator!= are defined. And the code works.

The begin() and end() of vector<T> are not input iterators but random
access iterators, right? Why I use find() on vectors, are there
something wrong?

There's a kind of hierachy among iterators that goes something like this:

Random Access > Bidirectional > Forward > Input/Output

meaning that a forward operator fulfills all the requirements of an
Input or Output iterator, a Bidirectional fullfills those of a Forward,
and so on. You could think of it like Forward iterators inherits from
Input/Output (they don't but the effect is the same).
 
L

lovecreatesbea...

Salt_Peter said:
(e-mail address removed) wrote:

Random access iterators will substitute for input iterators nicely.

Thank you. This dismisses my doubt on using begin() and end() iterators
of a vector with std::find().
 
L

lovecreatesbea...

Erik said:
There's a kind of hierachy among iterators that goes something like this:

Random Access > Bidirectional > Forward > Input/Output

meaning that a forward operator fulfills all the requirements of an
Input or Output iterator, a Bidirectional fullfills those of a Forward,
and so on. You could think of it like Forward iterators inherits from
Input/Output (they don't but the effect is the same).

Thank you. I wasn't aware of it when I first wrote those code using
std::find(). After read your post, I reread C++ Primer 4th and found
similar describing with in that book. I didn't read the book wholly
and didn't gain a complete concept on iterator categories.
 

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,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top