gpt4 book ai didi

c++ - 通过引用 std::inner_product 传递迭代器

转载 作者:行者123 更新时间:2023-11-30 03:53:01 28 4
gpt4 key购买 nike

考虑以下场景:

std::vector<int> a {1, 2, 3};
std::vector<int> b {1, 2, 3, 4};
auto it = b.begin();
auto res = std::inner_product(a.begin(), a.end(), it, 0); (*)

有没有办法将 it 传递给 std::inner_product 以便在 (*) 完成执行后 it 将指向 4。 (即 *it == 4)?

最佳答案

假设std::inner_product实现以线性方式递增第二个迭代器,您可以编写 std::vector<int>::iterator 的包装类可以传递给 std::inner_product并且这个包装器(它更新迭代器)间接地指向一个新位置。

但强烈建议不要使用这种方式,因为:

  1. 您正在对实现做出假设。
  2. 有很多更好的方法来递增迭代器。 std::advance(it, a.size());例如哪个应该是首选为你的情况。

关于c++ - 通过引用 std::inner_product 传递迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341057/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com