So I am trying to understand std:: search. First I created one array of a class and then copied to a vector.
Now I am trying to check if the contents of my vector appear in my array (i have modified one vector value, so they wouldn't be identical).
It is like the vector is empty, i cant cout the _brandName either!
This was my best try:
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <iterator>
class Car{
public:
Car(){};
Car(std::string brand, double speed){_brandName = brand; _speed = speed};
~Car(){};
bool operator==(const Car& rhs){return _brandName == _brandName;}
std::string GetBrand(){return _brandName;}
private:
std::string _brandName;
double _speed;
};
int main(){
Car carArray[4];
carArray[0] = Car("BMW", 280);
carArray[1] = Car("FORD", 300);
carArray[2] = Car("FORD", 380);
carArray[3] = Car("AUDI", 380);
auto arraySize = sizeof(carArray) / sizeof(carArray[0]);
std::vector<Car> carVector(carArray, carArray + arraySize);
carVector[0] = Car("Ferrari", 400);
std::cout << carVector[0].GetBrand();
std::vector<Car>::iterator it;
it = std::search(carVector.begin(), carVector.end(), std::begin(carArray), std::end(carArray));
std::cout << it->GetBrand();
return 0;
}
I had to add -1 to vector end otherwise I get the error: terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
I think that the error is because I am trying to call the std::string constructor with the value NULL but I don't understand why it is saying that.
I don't know if my implementation of the std::search is right either, what is the right way?