Check if element found in array c++ -
how can check if array has element i'm looking for?
in java, this:
foo someobject = new foo(someparameter); foo foo; //search through foo[] arr for(int = 0; < arr.length; i++){ if arr[i].equals(someobject) foo = arr[i]; } if (foo == null) system.out.println("not found!"); else system.out.println("found!");
but in c++ don't think i'm allowed search if object null c++ solution?
in c++ use std::find
, , check if resultant pointer points end of range, this:
foo array[10]; ... // init array here foo *foo = std::find(std::begin(array), std::end(array), someobject); // when element not found, std::find returns end of range if (foo != std::end(array)) { cerr << "found @ position " << std::distance(array, foo) << endl; } else { cerr << "not found" << endl; }
Comments
Post a Comment