I’m new to vector in C++, and I’m using pointer in it.
I’d like to search a variable if it already exists in the vector but I’m not sure how to do it.
B.cpp
vector<Animal*> vec_Animal;
vector<Animal*>::iterator ite_Animal;
What I’m trying to compare is Animal->getID();
And I have one more question.
Is there any way to make a limit when a user inputs value?
What I mean by is that if there’s a value year then, I want it to be typed 1000~2011 only. If user puts 999, it’d be wrong.
Is it possible?
Cheers
You can use the std::find_if algorithm.
Probably, You are using
std::vector::push_backor such methods to fill up the vector, These methods do not provide any checks, but one way to do achieve this is, by writing a small wrapper function inside which you check for the valid data conditions, and if the data is good then you add that in the vector or else you just return some error or throwstd::out_of_rangeexception from your wrapper function.Online Demo
Here is a minimilastic code sample, ofcourse you will need t tweak it further to suit your need:
Note that the sample is just an example of how to get
find_ifworking, it does not follow the best practices.