| noktalivirgul (41) | |
| how can i find the index of a certain number in a vector? | |
|
Last edited on
|
|
| vlad from moscow (3112) | |||
|
There is special standard algorithm that has name std::find For example std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; std::cout << std::distance( v.begin(), std::find( v.begin(), v.end(), 5 ) ) << std::endl; Here is a compiled example
The output is 4 | |||
|
Last edited on
|
|||
| noktalivirgul (41) | |||
my vector is defined so:vector<float>difflist;can i use this std also for this vector ?? and i am sorting my vector. for example my vector is:
ı sort my vector and it seems so: 5, 6, 9, 12, 37. now, how can i find the place of nine in orginal vector? if i use this way: std::cout << std::distance( v.begin(), std::find( v.begin(), v.end(), 5 ) ) << std::endl;will it give me 3 or2? | |||
|
|
|||
| vlad from moscow (3112) | |
| It will give you 0 because 5 is the first element of your vector after sorting. | |
|
|
|