Test if std::vector<> contains a specific entry
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Test if std::vector<> contains a specific entry

Hybrid View

  1. #1
    Join Date
    Aug 1999
    Posts
    492

    Test if std::vector<> contains a specific entry

    How do I test to see whether or not a vector contains a specific entry? I've looked all over the MSDN doc on std::vector and I don't see anything.

    For example:

    std::vector<int> myVector;
    myVector.push_back(10);
    myVector.push_back(20);
    myVector.push_back(30);

    Aside from looping through the vector manually, what do I do to determine if the vector contains the value 20?

    Thanks

  2. #2
    Join Date
    Apr 1999
    Posts
    27,431

    Re: Test if std::vector<> contains a specific entry

    Originally posted by SteveS
    How do I test to see whether or not a vector contains a specific entry? I've looked all over the MSDN doc on std::vector and I don't see anything.
    That is because what you are looking for is an STL algorithm that works for many different containers, not just vector.

    Use the std::find() algorithm. Note that std::find also works on plain old arrays too.
    Code:
    #include <vector>
    #include <iostream>
    #include <algorithm>
    
    int main()
    {
        std::vector<int> myVector;
        myVector.push_back(10);
        myVector.push_back(20);
        myVector.push_back(30);
       
        std::vector<int>::iterator it = std::find( myVector.begin(), myVector.end(), 20 );
        if ( it != myVector.end() )
        {
              std::cout << "Found the element at position " <<
                        std::distance( myVector.begin(), it ) << std::endl;
    
              std::cout << *it;
         }
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 1999
    Posts
    492
    Beautiful!

    Works perfectly! Thanks a lot!
    I have a list of C++ related books that I'm working through and STL is one of them. I think I'll put that one higher in the priority!

    Thanks

  4. #4
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Note that if the data is sorted it can be searched more efficiently. For small amounts of data it is efficient to search sequentially but for a large amount of data it could be quite significantly better if the search is not sequential.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  5. #5
    Join Date
    Apr 1999
    Posts
    27,431
    To add to Sam's post, if your data is sorted, you can use the binary_search() algorithm function.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center