1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <algorithm>
#include <iostream>
#include <vector>
std::vector<int*> findAll(int value, int numbers [], int length)
{
std::vector<int*> locations;
int* end = numbers + length;
int* cur = numbers;
while ((cur = std::find(cur, end, value)) != end)
locations.push_back(cur++);
return locations;
}
int main()
{
const std::size_t sz = 10;
int array[sz] = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 };
int val = array[0];
std::vector<int*> result = findAll(val, array, sz);
for (std::size_t i = 0; i < result.size(); ++i)
std::cout << i << ": " << *result[i] << " at " << result[i] - array << '\t';
std::cout << '\n';
}
| |