2017-03-06 48 views
-1

数组的元素的索引I具有用于存储非重复值的2D阵列和一些条目被随机挑选的push_back-ED到载体如喜爱列表。检索存储在向量

int num[10][10]; 
vector<int> fav_list; 
int retrieved_i, retrieved_j, retrieve_vector_position; 

for(i=0;i<10;i++) for(j=0;j<10;j++) 
// ...assign numbers to num... 

fav_list.push_back(num[2][3]); 
fav_list.push_back(num[4][7]); 
fav_list.push_back(num[6][2]); 
//...push_back more random selected num[...][...] into fav_list... 

的问题是,我怎么能检索i, j指数的特定fav_list[...]

我已努力使结构struct Num{int value, index_i, index_j;}num[10][10];让我能以这种方式

retrieved_i = fav_list[retrieve_vector_position].index_i; 
retrieved_j = fav_list[retrieve_vector_position].index_j; 

做,但我想知道有没有其他更好的/有效的方法?

+0

提供一个[MCVE]请。完全不清楚你问的是什么。 –

+0

你打算如何访问'fav_list'?通过随机索引或在'fav_list'中搜索某个值,然后使用找到的值的相应索引'(i,j)'? – sameerkn

+0

“retrieve_vector_position”决定的基础是什么? – sameerkn

回答

0

使用普通矢量存储你的2D阵列可以解决这个问题。您可以通过计算绝对索引(i * row_len + j)访问矢量中的元素并将其存储在fav_list绝对索引中。

此外,您可能需要使用std::unordered_mapfav_list。通常,散列表是这种缓存的最有效的数据结构。

0

有几种可能性取决于您想要访问指数/最爱号码本身的频率。

一种方法是,以保存索引代替的数量(或附加到它)。使用这种方法,需要更多的内存,但访问索引的时间将保持不变,无论数组的大小如何。这使用std::pair在您最喜欢的矢量中存储2个元素。

#include <vector> 
#include <iostream> 
#include <utility> 
using namespace std; 

int main(int argc, char* argv[]) { 
    int num[10][10]; 
    vector<std::pair<int, int>> fav_list; 
    for (int i = 0; i < 10; i++) { 
    for (int j = 0; j < 10; j++) { 
     if (/* your condition for favorites */) { 
     fav_list.push_back(num[i][j]); 
     } 
    } 
    } 
    /* example get the indices of the first favorite */ 
    cout << "i: " << fav_list[0].first << "j: " << fav_list[0].second << endl; 
    /* example get the first favorite */ 
    cout << num[fav_list[0].first][fav_list[0].second] << endl; 
    return 0; 
} 

另一种方法是“查找”的指标,当你需要它:它的条件,即一个数字是不包含在你的num[][]阵列多次(否则的第一个条目中找到)。不需要额外的内存开销,但是当数组变大时,查找索引的时间会增加。

#include <vector> 
#include <iostream> 
using namespace std; 

int main(int argc, char* argv[]) { 
    int num[10][10]; 
    vector<int> fav_list; 
    for (int i = 0; i < 10; i++) { 
    for (int j = 0; j < 10; j++) { 
     if (/* your condition for favorites */) { 
     fav_list.push_back(num[i][j]); 
     } 
    } 
    } 
    /* example get the indices of the first favorite */ 
    int indexI = -1, indexJ = -1; 
    for (int i = 0; i < 10; i++) { 
    for (int j = 0; j < 10; j++) { 
     if (fav_list[0] == num[i][j]) { 
     indexI = i; 
     indexJ = j; 
     break; 
     } 
    } 
    } 
    cout << "i: " << indexI << "j: " << indexJ << endl; 
    /* example get the first favorite */ 
    cout << fav_list[0] << endl; 
    return 0; 
} 
0

不是存储3变量value, x and y只是存储单个unsigned int通过它你可以检索x and y的。

struct fav_list 
{ 
    unsigned int total_rows; 
    unsigned int total_columns; 

    fav_list(unsigned int _rows, unsigned int _columns) 
    { 
     total_rows = _rows; 
     total_columns = _columns; 
    } 

    unsigned int get_x(unsigned int _index) 
    { 
     return v[_index]/total_columns; 
    } 
    unsigned int get_y(unsigned int _index) 
    { 
     return v[_index] % total_columns; 
    } 

    void append_xy_to_list(unsigned int _x, unsigned int _y) 
    { 
     v.push_back(_x * total_columns + _y); 
    } 
    vector <unsigned int> v; 
}; 


fav_list f(10, 10); 
for(x = 0; x < 10; ++x) 
{ 
    for(y = 0; y < 10; ++y) 
    { 
     //suppose you want to store the indexes of element num[x][y] then: 
     f.append_xy_to_list(x, y); 
    } 
} 

retrieved_i = f.get_x(retrieve_vector_position); 
retrieved_j = f.get_y(retrieve_vector_position);