12,070,171 members (29,120 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
Edited 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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 CHill60 275 OriginalGriff 173 Dave Kreskowiak 155 Richard Deeming 130 Sascha Lefèvre 110
 Dave Kreskowiak 2,276 OriginalGriff 1,624 Richard MacCutchan 1,535 CHill60 1,179 CPallini 1,122

Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 15 Jan 2013