我正在編寫一個代碼來減去矢量的相鄰元素,並將答案輸入到一個新矢量中。但是,我的代碼不起作用。究竟是什麼錯誤?C++抽象化矢量中的相鄰元素
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int length;
vector<int>values;
vector<int>values2;
cout << "Enter the length of the vector";
cin >> length;
values[0]=1; values[1]=2; values[2]=3; values[3]=4; values[4]=5;
for(int i=0; i<length; i++)
{
cout<<"Enter the " << i <<"th element of the vector";
cin >> values[i];
}
for (int i=0; i<length-1; i++)
{
values2[i]=values[i+1]-values[0];
}
return 0;
}
當某些「不工作」時,儘可能精確。它是否編譯?它打印的不是您預期的輸出嗎?它會崩潰嗎?向我們提供證據,證明您在尋求幫助之前真的自己搜索過。 – Aracthor