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を渡すことによって、再帰的に呼び出すことができる方法を知っていますか。
私は与えられたテキストファイル内の隣接する要素の合計を取得したい...三角形のパズルオイラープロジェクトの問題67. – andybandy12