【问题标题】:What syntax is used with adjacent difference?相邻差异使用什么语法?
【发布时间】: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++


【解决方案1】:

adjacent_difference 的参数都是迭代器。您正确提供的前两个:它们是输入范围的开始和结束。

第三个参数是一个输出迭代器,用于放置结果。例如:

#include <vector>
#include <numeric>

// Just some arbitrary inputs:
std::vector<int> inputs { 1, 2, 17, 15, 4, 19, 0, 2};
std::vector<int> results;

std::adjacent_difference(inputs.begin(), inputs.end(),
    std::back_inserter(results));

如果你只是想打印出结果,你可以直接通过为第三个参数指定一个 ostream_iterator 来做到这一点:

std::adjacent_difference(inputs.begin(), inputs.end(),
    std::ostream_iterator<int>(std::cout, "\t"));

但是请注意,您从std::adjacent_difference 获得的第一个值是第一个输入。随后的每个结果都是与前一个结果的差异。

另外请注意,您不需要为了找到最高和最低价格而对输入进行完全排序。最明显的方法是使用`std::minmax_element,如下所示:

auto hilo = std::minmax_element(inputs.begin(), inputs.end());

std::cout << "Lowest price: " << *hilo.first << "\n"
          << "highest price: " << *hilo.second << "\n";

【讨论】:

  • 你知道有什么方法可以丢弃第一个输出值吗?我在想也许应该有一个 std::inserter 丢弃第一个值,但没有运气找到它。目前,我刚刚复制了the reference implementation 并对其进行了编辑。
【解决方案2】:

您对adjacent_difference 的呼叫不正确。试试这个:

vector <int> p (15);
vector <int> result(15);

for (int i=0; i < p.size(); ++i) {
  cout << "Please enter a trading price." << endl;
  cin >> p[i];
}

adjacent_difference(p.begin(), p.end(), result.begin());

for (vector<int>::const_iterator i = result.begin(); i != result.end(); ++i) {
    cout << *i << ' ';
}

【讨论】:

  • 你好。谢谢!当我注释掉相邻差异的打印并编译时,我没有收到任何错误。我现在正在尝试打印计算出的相邻差值,但我很困惑并尝试了很多想法。你能解释一下我需要在原来的地方放什么吗:cout
【解决方案3】:

我想你定义(或导入)了一个函数“adjacent_difference”。但是编译器无法解析这两个过程调用:

 adjacent_difference(p.begin(), p.end(), p(it1));
 adjacent_difference(it1)

因此,基本上,基于参数,过程无法(明确地)决定为调用选择哪个构造函数。造成这种情况的原因可能是参数数量不匹配,或者参数本身不匹配(类型不同)。

C++ 使用“Koenig Lookup”来检查一个过程是否匹配一个调用。 我强烈建议你看看这些:Koenig LookupDetailed explanation

您必须知道可以存在完全匹配(参数类型完全匹配),但这不是必需的。编译器有时可以足够聪明地将其转换为所需的类型。

我猜第一个错误是因为编译器无法将迭代器转换为正确的类型。

但使用虚拟方法可能会更棘手。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
  • 2014-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多