2012-01-17 28 views
1
static int sum = 0; 
static int loop = 0; 

void put_into_vector(ifstream& ifs, vector<int>& v) 
{ 

    // String to store our file input string 
    string s; 

    // Extracts characters from the input sequence until a delimited is found 
    getline(ifs, s); 

    // Input string stream class to modify the strings 
    istringstream iss(s); 

    // Skip all the white spaces. 
    iss >> skipws; 

    // Function to check if stream's error flags (eofbit, failbit and badbit) are set. 
    if(iss.good()) 
    { 
     // Copies elements within the specified range to the container specified. 
     copy(istream_iterator<int>(iss), istream_iterator<int>(),back_inserter(v)); 
    } 
} 

void get_value(vector<int>& v, int start, int end) 
{ 
    while(loop < 4) 
    { 
     if(start == end) 
     { 
      sum = sum + v[start]; 
      loop++; 
      get_value(v,start,end+1); 
     } 
     if(v[start] > v[end]) 
     { 
      sum = sum + v[start]; 
      loop++; 
      get_value(v,start,end); 
     } 
     if(v[start] < v[end]) 
     { 
      sum = sum + v[end]; 
      loop++; 
      get_value(v,end,end+1); 
     } 
    } 
} 

int main() 
{  
    vector<int> triangle_array[4]; 
    ifstream ifs("numbers.txt"); 

    for(int i = 0; i < 4; i++) 
    { 
     put_into_vector(ifs, triangle_array[i]); 
    } 

    int row = 0; 
    get_value(triangle_array[row], 0, 0); 
    return 0; 
} 

私はコードを実行しようとしています。次のようにコードがあるテキストファイルを読み取ります関数の引数としてのベクトルの受け渡し

5

8 1

4 8 3

0 7 12 4

私はGET_VALUE関数を呼び出し、それが指す引数を渡します最初のベクトル はv [0] = 5です。start == endの最初の条件では、maxの値を更新しますが、d私は同じ関数をもう一度呼び出すが、次のベクトルieを渡したい。 v [1] には「8,1」があります。 v [1]やその中の何かを書いているときにエラーが出るので、私はこれを行うことができません。

エラーは次のとおりです。error C2664: 'get_value' : cannot convert parameter 1 from 'int' to 'std::vector<_Ty> &'

は、私はその後、vec[0]vec[1],vec[2]vec[3]すなわち次の行を指すVECを渡すことによって、再帰的に呼び出すことができる方法を知っていますか。

+0

私は与えられたテキストファイル内の隣接する要素の合計を取得したい...三角形のパズルオイラープロジェクトの問題67. – andybandy12

答えて

0

私はあなたが何をしたいのように少し困惑しているが、私はそれがこのようなものだ推測している:私はあなたに

get_value(triangle_array[row], 0, 0); this line 

を変更する必要があると思う

//you have 
void get_value(vector<int>& v, int start, int end); 

//you want 
void get_value(vector<int>* v, const int curVectorIdx, const int maxVectors, int start, int end) 
{ 
    int nextVectorIdx = curVectorIdx + 1; 
    if(nextVectorIdx < maxVectors) { 
     vector<int>* nextVector = v + nextVectorIdx; 
     get_value(nextVector, nextVectorIdx, maxVectors, nextVector->begin(), nextVector->end()); 
    } 
} 
+0

多分あなたは彼のテキストfもう一度。フォーマットが正しくないためにどのように見えたかははっきりしていませんでした。 (私はそれを再フォーマットし、私の改革がすぐに見えるようになることを願っています)。 –

+0

はいこれは正しいファイルです。フォーマットに感謝します。私は、最初の行に値を取得し、条件に応じて、次の行(ベクトルを使用)と開始と終了の変更値を取得します。しかし、私はまだそれを行う方法を理解することができません – andybandy12

+0

下の票が何であるか分かりません。あなたのget_value関数は単一のベクトル&を期待しています。 他の行を取得したい場合は、それを私が書いたものに似たものに変更する必要があります。 手動でアドレスをトラバースすることによって他の行を取得することは可能ですが、それを行うべきではないでしょう。 –

関連する問題