Click here to Skip to main content
12,754,175 members (30,231 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ MFC

Is it possible to get the position of an array?

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?

Posted 15-Jan-13 0:10am
Updated 15-Jan-13 0:14am
Mohibur Rashid 15-Jan-13 5:40am
are you familiar with vector?
epanjohura 15-Jan-13 5:48am
no, not really
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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.
Rate this: bad
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).
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2017
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