Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Searching a vector for a specific value

Reply
Thread Tools

Searching a vector for a specific value

 
 
Siegen
Guest
Posts: n/a
 
      09-21-2006
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
----------------------
---------------------



















 
Reply With Quote
 
 
 
 
shuisheng
Guest
Posts: n/a
 
      09-21-2006
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
> ----------------------
> ---------------------


 
Reply With Quote
 
 
 
 
aj@bookac.com
Guest
Posts: n/a
 
      09-21-2006
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
> ----------------------
> ---------------------


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-21-2006
In article <(E-Mail Removed)>, 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.
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      09-21-2006
On Fri, 22 Sep 2006 00:19:02 +1000 in comp.lang.c++, Siegen
<(E-Mail Removed)> 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/


 
Reply With Quote
 
Siegen
Guest
Posts: n/a
 
      09-24-2006
On Fri, 22 Sep 2006 00:19:02 +1000 Siegen <(E-Mail Removed)> wrote:

Thanks to:

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



















 
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
Searching a vector for a specific value Siegen C++ 13 09-25-2006 12:42 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



Advertisments