2016-11-03 13 views
2

以下は、グリッド内の各文字をカウントする関数です。 この関数は、各文字のカウントを返すようにしたいが、私は立ち往生している。どうすれば他の方法を処理するのに必要な数を返すことができるのでしょうか?関数の返す方法特定の数値を返す

int getNeighborhood(const char** grid, int N, int row, int col, int& bCount, int& fCount, int& rCount, int& gCount){ 
    int currRow; 
    int currCol; 
    int countB = 0; 
    int countF = 0; 
    int countR = 0; 
    int countG = 0; 

    //loop through all 8 grids surrounding the current row and column. 
    for(int i = -1; i < 2; i++)    
    { 
     for(int j = -1; j < 2; j++){ 
     currRow = row + i;    //current row. 
     currCol = col + i;    //current column. 

     if(currRow >= 0 && currRow < N && currCol >= 0 && currCol < N){ 
      if(grid[row][col] == 'B') 
      { 
       ++countB; 
      } 
      if(grid[row][col] == 'F') 
      { 
       ++countF; 
      } 
      if(grid[row][col] == 'R') 
      { 
       ++countR; 
      } 
      if(grid[row][col] == 'G') 
      { 
       ++countG; 
      } 
     } 
     } 
    //return statement required 
    } 

答えて

0

はたぶん、あなたは

std::map<char,int> getNeighborhood(const char** grid, int N, int row, int col){ 
    int currRow; 
    int currCol; 
    //contain all character to count 
    std::string chElementToCount = "BFGR"; 

    //The frequency is a map <char,int> correspond to <character to count, frequency> 
    std::map<char, int> frequency; 

    // intialize all frequency by 0 
    for (auto& ch: chElementToCount) { 
     frequency.insert(std::make_pair(ch,0)); 
    } 
    for(int i = -1; i < 2; i++)    
    { 
     for(int j = -1; j < 2; j++){ 
      currRow = row + i;    //current row. 
      currCol = col + i;    //current column. 

      // just get value of current char for easier later access 
      auto ch = grid[row][col]; 

      if(currRow >= 0 && currRow < N && currCol >= 0 && currCol < N){ 

       // the current char is a desired-to-count character then increase it frequency 
       if (chElementToCount.find(ch) != std::string::npos) 
        frequency[ch]++; 
      } 
     } 
    } 
    return frequency; 
} 

これは、STD ::マップhttp://www.cplusplus.com/reference/map/map/map/

+0

あまり知識がありません...私は掲示板として基本的に行く必要があります....配列plsを使って説明することができますか? – Atinuke

+0

@Atinuke編集済み! –

0

可能な解決策の1つはstd::tupleです。最初に関数のシグネチャを変更します。

std::tuple<int,int,int,int> getNeighborhood(const char** grid, int N, int row, int col) 

そして、このreturnステートメントを使用します。

return std::make_tuple(countB,countF,countR,countG); 

を、彼らはすべて同じタイプなので、あなたもstd::arrayを使用することがあります。

std::array<int,4> getNeighborhood(const char** grid, int N, int row, int col) 

この返信文を使用してください:

return std::array<int,4>{{countB,countF,countR,countG}}; 
+0

私はこれを試しましたが、役に立たない...サンプルコードは私の理解を助けるだろうpls。 – Atinuke

+0

「私はこれを試しましたが、助けにはならない」助けにならないのは何ですか?私は実際にコードを投稿しました!テストケースが必要ですか? –

0

あなたの質問は明確ではありませんが、私はbCount、fCount、rCount、gCountで文字カウントを取得すると仮定しています。カウントを取得するために1つの 使用ポインタは、何も

void getNeighborhood(const char** grid, int N, int row, int col, int *countB , int *countF , int *countR, int *countG) { 
int currRow; 
int currCol; 
*countB = 0; 
*countF = 0; 
*countR = 0; 
*countG = 0; 
for(int i = -1; i < 2; i++)    
{ 
    for(int j = -1; j < 2; j++){ 
    currRow = row + i;    
    currCol = col + i;    

    if(currRow >= 0 && currRow < N && currCol >= 0 && currCol < N){ 
     if(grid[row][col] == 'B') 
     { 
      ++*countB; 
     } 
     if(grid[row][col] == 'F') 
     { 
      ++*countF; 
     } 
     if(grid[row][col] == 'R') 
     { 
      ++*countR; 
     } 
     if(grid[row][col] == 'G') 
     { 
      ++*countG; 
     } 
    } 
    } 

} 

今すぐ各文字は

ソリューションを数えるのはポインタ渡しの関数を呼び出す返さない2つの解決策があります

ソリューション2 各文字の数を保持し、関数が配列を返す配列を作成します。

+0

は、この関数を呼び出すには--- getNeighborhood(const char ** grid、int N、int row、int col、int * countB、int * countF、int * countR、int * countG)となります。 ? – Atinuke

+0

char count変数を作成したとします。 int countB、countF、countR、countG; getNeighborhood(const char ** grid、int N、int row、int col、&countB、&countF、&countR、&countG) 関数が返るときは、それぞれの変数に必要な値を設定する必要があります。 –

+0

@Atinukeはあなたのために働いたのですか? –

0

を使用するための一例であるのstd ::マップを使用することができますどのような、例えば不正な引数、で呼び出された場合、あなたの関数の戻り値を希望row > N-2?この例は、あなたの関数が失敗する可能性があることを示しています。

あなたの関数が成功/失敗といくつかの結果値を報告しなければならない場合、成功/失敗情報は通常、関数の戻り値とともに報告されます。結果の値はOUT引数で報告されます。

これは、構造体を使用して結果値を集計する次のコードにつながります。

#include <cstdint> 
#include <iostream> 
//#include <vector> 

struct CountResult 
{ 
    size_t B; 
    size_t F; 
    size_t R; 
    size_t G; 
    CountResult() 
     : B(0) 
     , F(0) 
     , R(0) 
     , G(0) 
    {} 
    void Reset() 
    { 
     B = 0; F = 0; R = 0; G = 0; 
    } 
}; 

std::ostream& operator<<(std::ostream& stm, const CountResult& x) 
{ 
    stm << "{ B = " << x.B << ", F = " << x.F << ", R = " << x.R << ", G = " << x.G << "}"; 
    return stm; 
} 

bool GetNeighborhood(const char * const * const grid, size_t N, size_t row, size_t col, CountResult & result) 
{ 
    if (nullptr == grid) 
     return false; 
    // Range check. Operation only allowed for squares with a full border. 
    if (row > (N - 2) || row < 1 || col > (N - 2) || col < 1) 
     return false; 

    result.Reset(); 

    for(int16_t r = -1; r < 2; r++) 
     for (int16_t c = -1; c < 2; c++) 
     { 
      if (!(r == 0 && c == 0)) 
      { 
       switch (grid[row + r][col + c]) 
       { 
       case 'B': result.B++; break; 
       case 'F': result.F++; break; 
       case 'R': result.R++; break; 
       case 'G': result.G++; break; 
       default: // illegal value in grid! 
        return false; 
       } 
      } 
     } 
    return true; 
} 

int main(int argc, const char *argv[]) 
{ 
    const size_t N = 11; 
    char **grid = new char *[N]; // TODO: check for nullptr (out of memory) 
    for (size_t r = 0; r < N; r++) 
    { 
     grid[r] = new char[N]; // TODO: check for nullptr (out of memory) 
     for (size_t c = 0; c < N; c++) 
      grid[r][c] = 'G'; 
    } 

    CountResult result; 
    if (GetNeighborhood(grid, N, 5, 5, result)) 
    { 
     std::cout << "Result = " << result << std::endl; 
    } 
    else 
    { 
     std::cout << "GetNeighborhood() returned false." << std::endl; 
    } 

    // TODO: delete[] the rows and the grid itself. 

    return 0; 
} 

このコードは主に実行されているため、ヒープ割り当てグリッドのクリーンアップコードを記述するのは面倒ではありませんでした。通常、それを行う必要があります。

通常、私がTODO:ステートメントで暗示したすべての低レベルメモリリークチェックコードを避けるために、通常はstd::vector< std::vector<char> >を使用します。

関連する問題