Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 14-Jan-13 23:14pm
v2
Comments
Mohibur Rashid 15-Jan-13 5:40am    
are you familiar with vector?
epanjohura 15-Jan-13 5:48am    
no, not really

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
 
Share this answer
 
Comments
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
chandanadhikari 15-Jan-13 6:21am    
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.
You may use std::find[^] for that (or simply write yourself the simple serach function).
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900