I have a vector like below.
struct Personel {
uint16_t personelId;
uint8_t age;
}
std::vector<Personel> Personels;
Imagine adding 1000 elements as below.
Personel personel1;
personel1.personelId = 1;
personel1.age = 26;
Personels.push_back(personel1);
.
.
.
Personel personel1000;
personel1000.personelId = 1000;
personel1000.age = 42;
Personels.push_back(personel1000);
As it is understood from the code, personelId
will be unique.
Without iterating each element with for and comparing, How can I get index according to personelId
.
Below code not working: Because int
not Personel
auto match = std::find(Personels.begin(), Personels.end(), 596);
int index = std::distance(Personels.begin(), match); //must return 596