2017-03-06 17 views
-1

2次元配列には反復されない値が格納され、いくつかのエントリはランダムに選択され、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... 

問題は、私は特定のfav_list[...]i, jインデックスを取得する方法、ありますか?

、他のより良い/効率的な方法がある私は、このように

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

を行うことができるように構造体struct Num{int value, index_i, index_j;}num[10][10];を作ってみましたが、私は知りたいですか?

+0

[MCVE]を提供してください。あなたが何を求めているかは完全には不明です。 –

+0

'fav_list'にどうやってアクセスしますか?ランダムなインデックスを付けるか、 'fav_list'で何らかの値を検索し、見つかった値の対応するインデックス'(i、j) 'を使用するか? – sameerkn

+0

'retrieve_vector_position'が決定される根拠は何ですか? – sameerkn

答えて

0

プレーンベクターを使用して2D配列を格納すると、問題が解決します。絶対インデックス(i * row_len + j)を計算し、絶対インデックスにfav_listを格納することによって、ベクトルの要素にアクセスできます。

また、fav_liststd::unordered_mapを使用することもできます。一般に、ハッシュテーブルは、そのようなキャッシュのための最も効率的なデータ構造です。

0

i & j個のインデックス/お気に入り番号にアクセスする頻度によって、いくつかの可能性があります。

1つのアプローチは、番号の代わりにインデックスを(またはそれに追加して)保存することです。このアプローチでは、より多くのメモリが必要ですが、インデックスにアクセスする時間は、配列の大きさに関係なく一定です。これは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; 
} 

別のアプローチは、あなたがそれを必要とするときに、「検索」のインデックス、です:それは条件があり、1つの数があなたのnum[][]配列に含まれる複数回ではないこと(そうでない場合は最初のエントリが発見されました)。追加のメモリオーバーヘッドは必要ありませんが、配列が大きくなるとインデックスを参照する時間が長くなります。代わりに、ちょうどあなたがx and yを取り出すことができ、それを介して単一unsigned intを保存する3変数value, x and yを格納する

#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

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); 
関連する問題