1

I have an array which holds a structure like:

struct Point
{
int x;
int y;
}

Point array_of_structure[10] ;

for(int i=0;i<10;i++)
{
  array_of_structure[i].x = i*2;
}

I want to get the structure which holds x value of 6. In this way I access the y value of that structure. How can I do it? It's something like the below:

Point p = Get the structure which contains x value of 6;
int c = p.y;

Here is a sample solution. But I need a better idea or ideas.

for(int i=0;i<10;i++)
   if(array_of_structure[i].x==6)
      return array_of_structure[i].y;

I thought about maybe pointers make this job but I'm not sure about it. I cannot figure out how to solve this problem.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
oiyio
  • 5,219
  • 4
  • 42
  • 54
  • Do you know how to iterate over an array? Do you know how to use pointers? – Beta Nov 24 '13 at 01:14
  • yes . But i wonder whether an efficient solution , a standard library function , or a wise way exist for that problem or not – oiyio Nov 24 '13 at 01:17
  • There might be notationally more compact ways to do it, but they're essentially shorthands for a linear search. If you're authorized to assume that the data is sorted, then you can look at binary search. Your initialization sets the x-coordinates in increasing order, but it doesn't set the y-coordinates at all, so it isn't clear what you expect to happen. – Jonathan Leffler Nov 24 '13 at 01:17
  • Actually x values are id's. And i want to access the other information inside the structure which is equal to the given id. – oiyio Nov 24 '13 at 01:23
  • You must learn the basics before you attempt to master advanced techniques. If you don't know *any* way to solve this problem, don't worry about efficiency or look for a standard function. – Beta Nov 24 '13 at 01:25
  • I work with big size of arrays like 1000-2000. For each searching i make scanning 1000-2000 times. It means in total 1000.1000 = 1000000 , 2000.2000 = 4000000 scanning. So it will be very unefficient unfortunately. I hope it doesn't matter. – oiyio Nov 24 '13 at 08:59

1 Answers1

2

Standard library provides a function std::find_if which can be used to find an item without a loop. As a learning exercise, however, you could do it using a loop as described below:

You can iterate your array of structs until you find the x of interest. You can use a pointer or an index, depending on your preferences. You need to set a flag indicating if you have found your item or not.

Here is how you can do it with a pointer:

struct Point *ptr;
bool found = false;
for (ptr = array_of_structure ; !found && ptr != &array_of_structure[10] ; ptr++) {
    found = (ptr->x == x);
}
if (found) {
    cout << ptr->y << endl;
}

Here is how you can do it with an index:

int index ;
bool found = false;
for (index = 0 ; !found && index != 10 ; index++) {
    found = (array_of_structure[index].x == x);
}
if (found) {
    cout << array_of_structure[index].y << endl;
}

Note: if you are looking for a find_if solution, here is an answer that explains this approach.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523