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對此我想再次調用相同的函數,但傳遞下一個向量即。 v [1] 其中有「8,1」。我不能這樣做,因爲它在寫入v [1]或其中的任何內容時給我一個錯誤。
的錯誤是:error C2664: 'get_value' : cannot convert parameter 1 from 'int' to 'std::vector<_Ty> &'
你知道的一種方法,我可以通過傳遞指向的VEC到下一行,即vec[0]
然後vec[1],vec[2]
和vec[3]
遞歸調用它。
我想獲得在給定的文本文件中的相鄰元素的總和...三角形智力玩具歐拉項目問題67. – andybandy12 2012-01-17 19:40:24