lovecreatesbea...@gmail.com wrote:
> 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:
perator==
> and T:
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?