12,754,175 members (30,231 online)
Rate this:
See more:
Hi,

Is it possible to get the position of an array?

Example:
```int iArray[] = {2, 3, 10, 35, 5, 87, 9, 6};
iValue = m_iValue    // m_iValue = 5 -> from database
```

Now i got "iValue" but I want to "search" this value in "iArray" and get its position - here it would be iValue = 5 so the position must be 4 => iArray[4].

Is there any method in C++ for that?

Greetings
Epanjohura
Posted 15-Jan-13 0:10am
Updated 15-Jan-13 0:14am
v2
Mohibur Rashid 15-Jan-13 5:40am

are you familiar with vector?
epanjohura 15-Jan-13 5:48am

no, not really

Rate this:

## Solution 2

hi,
i think you have an array of numbers and you want to check if the entered value is present in the array and if it is present then at which location. you want to do this by using some method which c++ already provides. (please correct if i am wrong !)

you can do this easily by using vector[^] instead of an array

see here[^] on how to use std:distance algorithm to find the index of an iterator
a similar question is here on index of an iterator[^]

check how to find value in a vector here[^]
if you still want to use an array look here[^]

hope this helps
epanjohura 15-Jan-13 6:12am

Yes, that's right. I still want to use an array because I need it the whole code.
Thank you very much for your answer and I will try it now.

Greetings

you can check the last link (it has some code also) and please vote for the solutions if you found them helpful-thanks
epanjohura 15-Jan-13 7:57am

It was very helpful, thank you a lot.
Rate this:

## Solution 1

You may use std::find[^] for that (or simply write yourself the simple serach function).
epanjohura 15-Jan-13 5:48am

well thanks for your help, I'll try it

Top Experts
Last 24hrsThis month
 Graeme_Grant 315 ppolymorphe 206 Maciej Los 170 Peter Leow 160 Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ) 150
 OriginalGriff 3,971 Peter Leow 3,051 Karthik Bangalore 2,375 ppolymorphe 2,353 Richard MacCutchan 1,594