I need to get number of character in array.
const char myarray[5] = {'0', 'a', 'e', 'f', 'c'}; // Create array of char
int number=0; // Create variable
number = getposition(myarray, 'f'); // Now number equals to 3
number = getposition(myarray, 'z'); // -1, because array doesn't have this char
My task is easy because array don’t have repeating characters (for example, it can’t be smth like this: {‘a’, ‘1’, ‘f’, ‘a’}). How can I do it?
A little bit more C++:
A lot more C++:
Bonus C++11/C++11 update
Bonus C++17 update
Here, the original question gets direct support in
std::string_view:Live On Coliru