Чуть больше C ++:
#include <algorithm>
int getposition(const char *array, size_t size, char c)
{
const char* end = array + size;
const char* match = std::find(array, end, c);
return (end == match)? -1 : (match-array);
}
намного больше C ++:
template <typename T, size_t N>
int getposition(const T (&array)[N], const T c)
{
const T* match = std::find(array, array+N, c);
return (array+N==match)? -1 : std::distance(array, match);
}
Бонус C ++ 11 / C ++ 11 обновление
#include <algorithm>
#include <iterator>
template <typename Range, typename T>
size_t index_of(Range const& range, T const& c) {
using std::begin;
using std::end;
auto b = begin(range), e = end(range);
auto match = std::find(b, e, c);
return (e==match)? -1 : std::distance(b, match);
}
Бонус C ++ 17 обновление
Здесь оригинальный вопрос получает прямую поддержку в std::string_view
:
Live On Coliru
#include <string_view>
using namespace std::string_view_literals;
int main() {
return "hello"sv.find('e');
}