【问题标题】:C++ function return type [closed]C ++函数返回类型
【发布时间】:2016-05-09 03:54:07
【问题描述】:

使用下面的函数原型,如何从列表中返回一个项目?

std::list<int> &GD (int);

【问题讨论】:

  • 签名意味着您要返回整个列表...?
  • 也许std::advance 是您要找的?虽然我不清楚你给出的例子的确切问题。
  • 您想从列表中获取哪一项?首先?最后?他们都是?如果答案是“具有随机索引的项目”,那么您可能想要的不是std::list

标签: c++ function function-prototypes


【解决方案1】:

要访问列表的元素,您可以调用如下函数:

int get(int index, std::list<int> const& A) {
    int i = 0;
    auto pos = A.begin();
    while(i++ < index) pos = std::next(pos);
    return *pos;
}

【讨论】:

  • std::list 没有运算符[]。
猜你喜欢
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 2019-08-08
  • 2015-04-30
  • 2013-02-18
  • 1970-01-01
  • 2017-08-18
相关资源
最近更新 更多