Searching a vector for a specific value

Discussion in 'C++' started by Siegen, Sep 21, 2006.

  1. Siegen

    Siegen Guest

    Hi.

    Could anyone tell me how one can search a vector for a specific value?

    std::vector<int> vec;

    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);

    How to check whether the number three is in the vector?


    I tried "find" but it doesn't appear to be available to "vector".


    Any help appreciated.

    Regards,

    -- Siegen
    ----------------------
    ---------------------
     
    Siegen, Sep 21, 2006
    #1
    1. Advertising

  2. Siegen

    shuisheng Guest

    Please see http://www.cppreference.com/cppalgorithm/find.html. It may
    give you some hint.

    Siegen wrote:
    > Hi.
    >
    > Could anyone tell me how one can search a vector for a specific value?
    >
    > std::vector<int> vec;
    >
    > vec.push_back(1);
    > vec.push_back(2);
    > vec.push_back(3);
    > vec.push_back(4);
    >
    > How to check whether the number three is in the vector?
    >
    >
    > I tried "find" but it doesn't appear to be available to "vector".
    >
    >
    > Any help appreciated.
    >
    > Regards,
    >
    > -- Siegen
    > ----------------------
    > ---------------------
     
    shuisheng, Sep 21, 2006
    #2
    1. Advertising

  3. Siegen

    Guest

    std::vector<int>::iterator iter = find(vec.begin(), vec.end(), 3);

    if (iter1 != vec.end())
    ..
    ..
    ..



    Siegen wrote:
    > Hi.
    >
    > Could anyone tell me how one can search a vector for a specific value?
    >
    > std::vector<int> vec;
    >
    > vec.push_back(1);
    > vec.push_back(2);
    > vec.push_back(3);
    > vec.push_back(4);
    >
    > How to check whether the number three is in the vector?
    >
    >
    > I tried "find" but it doesn't appear to be available to "vector".
    >
    >
    > Any help appreciated.
    >
    > Regards,
    >
    > -- Siegen
    > ----------------------
    > ---------------------
     
    , Sep 21, 2006
    #3
  4. Siegen

    Jerry Coffin Guest

    In article <>, Siegen092006
    @end.of.message says...
    > Hi.
    >
    > Could anyone tell me how one can search a vector for a specific value?


    std::find would be the most obvious choice.

    > std::vector<int> vec;
    >
    > vec.push_back(1);
    > vec.push_back(2);
    > vec.push_back(3);
    > vec.push_back(4);


    It's not clear whether you intended to imply that the vector would be
    sorted, or it was just coincidence that the items in your demo vector
    happened to be in order. If your vector will be sorted, you can use
    std::binary_search instead.

    > How to check whether the number three is in the vector?


    Since your vector is sorted:

    if (std::binary_search(vec.begin(), vec.end(), 3))
    // 3 is present
    else
    // 3 isn't present

    If it might not be sorted:

    if (std::find(vec.begin(), vec.end(), 3) != vec.end())
    // 3 is present
    else
    // 3 isn't present

    > I tried "find" but it doesn't appear to be available to "vector".


    I'd guess you were looking for a member function, and you're right,
    there is no vector::find(). The member function is provided for those
    containers (e.g. [multi]{set|map} ) are always sorted so you can always
    do a binary search. A vector might or might not be sorted, so it's up to
    you to decide whether to use a linear search (std::find) or a binary
    search (std::binary_search).

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 21, 2006
    #4
  5. Siegen

    David Harmon Guest

    On Fri, 22 Sep 2006 00:19:02 +1000 in comp.lang.c++, Siegen
    <> wrote,
    >How to check whether the number three is in the vector?
    >
    >I tried "find" but it doesn't appear to be available to "vector".


    "find" is fine. You didn't post what you tried, so how in the world
    is anybody supposed to be able to tell you what's wrong with it!

    This issue is covered in Marshall Cline's C++ FAQ. See the topic
    "[5.8] How do I post a question about code that doesn't work
    correctly?" It is always good to check the FAQ before posting.
    You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/
     
    David Harmon, Sep 21, 2006
    #5
  6. Siegen

    Siegen Guest

    On Fri, 22 Sep 2006 00:19:02 +1000 Siegen <> wrote:

    Thanks to:

    shuisheng

    Jerry Coffin
    David Harmon

    For replying.

    You have clarified the matter for me.

    I got this close before posting my original message.

    std::vector<int>::iterator iter = vec.find(vec.begin(), vec.end(), 3);

    Thanks again and cheers,



    :> Hi.
    :>
    :> Could anyone tell me how one can search a vector for a specific value?
    :>
    :> std::vector<int> vec;
    :>
    :> vec.push_back(1);
    :> vec.push_back(2);
    :> vec.push_back(3);
    :> vec.push_back(4);
    :>
    :> How to check whether the number three is in the vector?
    :>
    :>
    :> I tried "find" but it doesn't appear to be available to "vector".
    :>
    :>
    :> Any help appreciated.
    :>
    :> Regards,
    :>
    :> -- Siegen
    :> ----------------------
    :> ---------------------
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>
    :>

    -- Siegen
    ----------------------
    ---------------------
     
    Siegen, Sep 24, 2006
    #6
    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. pmatos
    Replies:
    6
    Views:
    23,953
  2. Replies:
    8
    Views:
    1,967
    Csaba
    Feb 18, 2006
  3. Siegen
    Replies:
    13
    Views:
    512
    Kai-Uwe Bux
    Sep 25, 2006
  4. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    375
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page