user2827150
user2827150

Reputation: 11

How to get indexes for values in array

I need to write a function that takes as argument 2d array. Each element of array has an Integer value. As input i have a 2D array for example:

[1][1][2][2]
[2][1][2][2]
[3][3][3][3]
[22][.......

and as output I need to store indexes for each value :

value = 1 : [0,0] ; [0,1] ; [ 1,1]
value = 2 : [1,0] ; ....
value = 3 : [2,0] ; .......
value  = 22 : [.........

Size of array may be various, same as number of values. Is it somehow possible to save that data to vector, or any other data type so later i could read those values and their indexes?

Sorry if something is unclear, Its my 1st post here:) Cheers

Edit:

so ok what I tried to do: I created a class Indexes

class Indexes
{
public:
    int x;
    int y;
}; 

and later i created a vector vect; and i tried to add indexes to that vector. The problem was when i tried to keep values separated for examlpe

for(int i=0 ; i<size ; i++){
     for(int i=0 ; i<size ; i++){
        if(array[i][j].value = 1)
           Indexes ind(i,j);
       vect.push_back(ind);
     }
}

but all I could get is vector with only 1 values and its indexes stored;

Upvotes: 1

Views: 693

Answers (1)

Asaf
Asaf

Reputation: 4407

You can store this index in a map of vectors of pairs, for example.

Here's some sample code:

typedef std::pair<int, int> ElementIndex;
typedef std::vector<ElementIndex> IndexList;
typedef std::map<int, IndexList> ValuesIndexMap;

So ValuesIndexMap is a map from value to a vector of all indices in which this value is stored in, where an index (type ElementIndex) is a pair of ints - row and column.

Upvotes: 1

Related Questions