Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can find() apply on vector

Reply
Thread Tools

Can find() apply on vector

 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      12-16-2006
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.

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);

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      12-16-2006

(E-Mail Removed) 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?

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      12-16-2006
On 2006-12-16 16:25, (E-Mail Removed) 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.
>
> 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).

--
Erik Wikström
 
Reply With Quote
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      12-18-2006

Salt_Peter wrote:
> (E-Mail Removed) wrote:

<snip>
> > 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.


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

 
Reply With Quote
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      12-18-2006

Erik Wikström wrote:
> On 2006-12-16 16:25, (E-Mail Removed) 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.
> >
> > 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).


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
[XSLT] could not apply "apply-templates" Stefan Siegl XML 1 07-18-2003 09:43 AM



Advertisments