Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC
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
Comments
Mohibur Rashid at 15-Jan-13 5:40am
   
are you familiar with vector?
epanjohura at 15-Jan-13 5:48am
   
no, not really
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Comments
epanjohura at 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 at 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 at 15-Jan-13 7:57am
   
It was very helpful, thank you a lot.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You may use std::find[^] for that (or simply write yourself the simple serach function).
  Permalink  
Comments
epanjohura at 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)

  Print Answers RSS
0 OriginalGriff 375
1 Sergey Alexandrovich Kryukov 329
2 CPallini 260
3 DamithSL 214
4 Maciej Los 197
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100