【发布时间】:2015-04-19 01:50:50
【问题描述】:
我正在编写一个 C++ 程序来确定最低和最高交易价格,并通过向用户查询 15 个交易价格来确定这些价格之间的差异,通过使用相邻差异计算差异,打印出差异,计算通过使用排序记录的最低和最高价格,并打印出这些值。
我熟悉排序的使用,但我不熟悉相邻的差异。我读了几遍教科书,搜索了互联网并多次尝试更改我的代码,但我仍然遇到错误并且迷失了方向。
这是我的代码:
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int main (void)
{
vector <int> p (15);
vector <int>::iterator it1;
cout<<"This program determines the daily differences, lowest and highest of 15 user-entered trading prices."<<endl;
for (int i=0; i<p.size(); i++)
{
cout<<"Please enter a trading price."<<endl;
cin>>p[i];
}
adjacent_difference(p.begin(), p.end(), p(it1));
for (int i=0; i<p.size(); i++)
{
cout<<"The difference in daily trading prices is: "<<adjacent_difference(it1)<<endl;
}
sort (p.begin(), p.end());
cout<<endl;
cout<<"The lowest trading price recorded was "<<p[0]<<"."<<endl;
cout<<"The highest trading price recorded was "<<p[14]<<"."<<endl;
return 0;
}
以下是错误:
- 28:47:错误:不匹配调用“(std::vector) (std::vector::iterator&)” 相邻差异(p.begin(),p.end(),p(it1));
- 31:81:错误:没有匹配函数调用“adjacent_difference(std::vector::iterator&)” cout
有人能解释一下所涉及的语法吗? 谢谢!
【问题讨论】:
-
reference - 它需要一个目标来写入,就像第一个参数是从哪里开始读取一样。那里还有一个示例,您可以运行和编辑。
-
p(it1)应该是什么?p是一个向量,而不是一个函数。 -
@Barmar 我正在尝试根据相邻差异的语法添加第三个目的地,但我不理解语法,所以我寻求帮助。 (:
-
@reference 好的...我想我有点明白了。你能解释一下吗?
-
adjacent_difference只是一个函数,它的语法和其他函数一样。
标签: c++