3

I'm trying to understand this line of code

vector<int>::iterator it = find(list_vector.begin(), list_vector.end(), 5)

where I have vector<int> list_vector; declared before hand.

What does the 5 do? What does it return? Does it return the 5 if it can find it at the beginning and end? If I wanted to make an if statement, and I wanted to find if the number 10 was in the statement (if it was, return true) how would I go about doing that?

Jake Smith
  • 189
  • 2
  • 2
  • 11
  • 8
    http://en.cppreference.com/w/cpp/algorithm/find – us2012 Sep 26 '13 at 02:28
  • 1
    I think that "RTFM", harsh as it may seem, is the only appropriate answer here... – Matteo Italia Sep 26 '13 at 02:29
  • "find if the number 10 was in the [if] statement"? Huh? – aschepler Sep 26 '13 at 02:30
  • I apologize for the "RTFM". I tried looking up the answer and couldn't find anything. I can't seem to do if (vector::iterator it = find(plist.begin(), plist.end(), number) == number) as it is giving errors. Any ideas? – Jake Smith Sep 26 '13 at 02:37
  • An iterator mimics a pointer; you'd have to *dereference* it to get back the number. That is, of course, if the iterator isn't `plist.end()` (the iterator equivalent of `NULL`). – chrisaycock Sep 26 '13 at 02:38
  • @JakeSmith - that is not a valid syntax in C++. A declaration cannot be part of an expression. If you want to delcare `it` *and* compare it to some value, you must do that in two distinct statements. – Robᵩ Sep 26 '13 at 02:39
  • possible duplicate of [C++ comparing a string with an array of strings](http://stackoverflow.com/questions/18742637/c-comparing-a-string-with-an-array-of-strings) – Yakk - Adam Nevraumont Sep 26 '13 at 02:47

1 Answers1

10
vector<int>::iterator it = find(list_vector.begin(), list_vector.end(), 5)

std::find searches in the range defined by its first two arguments. It returns an iterator pointing to the first element that matches. If no element matches, it returns its 2nd parameter.

list_vector.begin() returns an iterator that points to the first element of list_vector.

list_vector.end() returns an iterator that points one element beyond the final element of list_vector.

5 is the target of the search. find() will look for an element that has the value 5.

If you'd like to determine if 10 is present anywhere in the vector, do this:

if(std::find(list_vector.begin(), list_vector.end(), 10) == list_vector.end())
    std::cout << "No 10, bummer\n";
else
    std::cout << "I found a 10!\n";

Or, if you'd like to simultaneously determine if 10 is present and determine its location:

std::vector<int>::iterator it = std::find(list_vector.begin(), list_vector.end(), 10);
if(it == list_vector.end())
    std::cout << "No 10\n";
else
    std::cout << "Look what I found: " << *it << "\n";
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • For the first one, it will search from beginning to the end for 10, how come when you are doing == you can't set it to say, 10, 5, or just some int variable that I let the user enter? It's giving me errors when I try that. for example, find(list_vector.begin(), list_vector.end(), 10) == number) – Jake Smith Sep 26 '13 at 02:46
  • @JakeSmith, `std::find` returns an iterator, not whatever type the third argument is. – chris Sep 26 '13 at 02:49
  • how do you find next occurrence using find – Muhammad Umer Oct 03 '14 at 05:42
  • 1
    @MuhammadUmer - `iterator it = std::find(v.begin(), v.end(), 10); std::cout << "First find: " << *it << "\n"; it = std::find(it, v.end(), 10); std::cout << "Second find: " << *it << "\n";` – Robᵩ Oct 03 '14 at 13:45
  • lol that was simple thanks – Muhammad Umer Oct 03 '14 at 14:30