### find index in a vector-help urgent!

how can i find the index of a certain number in a vector?
Last edited on
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

 ``1234567891011`` ``````#include #include #include #include int main() { std::vector 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; }``````

The output is

4
Last edited on
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:

 ``123`` `````` vectordifflist; difflist.push_back(5, 6, 12, 9, 37)``````

ı 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?
It will give you 0 because 5 is the first element of your vector after sorting.
Topic archived. No new replies allowed.