Everything @Jerry said.
In addition I echo the view of @Bizket but disagree with the better solution.
C++ emphasize the use of concepts (the exact type does not matter, the interface of the type is what matters when designing). You loop over an C-array and Bizkit says you should update to specific C++ container types. I would suggest that you update to use generic C++ container types (as long as your input acts like a container) that support the interface you need (then let the compiler verify its correct).
Also C++ tends to use Iterators
to define ranges (or containers). This allows you to be more flexible issue.
Also why are you constraining your algorithm to int
? Just make it generic then you don't need to write it again for other types.
template<typename C, typename V>
std::vector<std::size_t> searchList(C const& theContainer, V const& findFor)
{
return searchList(std::begin(theContainer), std::end(theContainer), findFor);
}
template<typename I>
std::vector<std::size_t> searchList(I begin, I end, std::iterator_traits<I>::value_type const& findFor)
{
std::vector<std::size_t> result;
for(std::size_t index = 0; begin != end; ++begin, ++index)
{
if ((*begin) == findFor)
{
result.push_backemplace_back(index);
}
}
return result;
}
Note: the exact type does not matter
. Type is still the most important thing of a C++ object. But the exact type does not matter just that it has a specific type and that you can determine the interface of the type at compile time.