Can find() apply on vector

Discussion in 'C++' started by lovecreatesbea...@gmail.com, Dec 16, 2006.

  1. Guest

    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);
     
    , Dec 16, 2006
    #1
    1. Advertising

  2. Salt_Peter Guest

    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::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?
     
    Salt_Peter, Dec 16, 2006
    #2
    1. Advertising

  3. On 2006-12-16 16:25, 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::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).

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Dec 16, 2006
    #3
  4. Guest

    Salt_Peter wrote:
    > 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().
     
    , Dec 18, 2006
    #4
  5. Guest

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


    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.
     
    , Dec 18, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Siegl
    Replies:
    1
    Views:
    994
    Marrow
    Jul 18, 2003
  2. pmatos
    Replies:
    6
    Views:
    24,110
  3. Replies:
    8
    Views:
    1,997
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    619
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    392
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page