【发布时间】:2014-02-03 17:29:45
【问题描述】:
我正在学习 C++,所以我觉得这应该是一个非常简单的答案——但我似乎找不到。所以如果我太天真了,我提前道歉。
我有一个std::vector<int> of 值,我正在尝试查找 odd 值的索引。
我正在关注here的代码:
(以下重复):
// find_if example
#include <iostream> // std::cout
#include <algorithm> // std::find_if
#include <vector> // std::vector
bool IsOdd (int i) {
return ((i%2)==1);
}
int main () {
std::vector<int> myvector;
myvector.push_back(10);
myvector.push_back(25);
myvector.push_back(40);
myvector.push_back(55);
std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
std::cout << "The first odd value is " << *it << '\n';
return 0;
}
本示例打印第一个奇数值。 我如何扩展它以给我myvector 中每个奇数值的索引值?这是正确的方法吗?
【问题讨论】: