1

How can I get the index an item in a std::array without running any loops?

#include <iostream>
#include <array>

std::array<int, 10> some_array = { 89, 56, 78, 96, 4, 34, 77, 2, 48, 3};

unsigned int GetIndexOfValue(unsigned int some_value) {
    // How get the index of some_value here without running a loop?
}

int main() {
    unsigned int some_value = 34;
    std::cout << "The index of value passed is " << GetIndexOfValue(some_value) << std::endl;
}

Is it possible to do it using std::find?

3 Answers 3

4

You can use the functionality from the <algorithm> header, so you can avoid writing a raw loop, like this:

unsigned int GetIndexOfValue(unsigned int some_value) {
    return std::distance(std::begin(some_array),
             std::find(std::begin(some_array), std::end(some_array), some_value));
}

Here's a demo.

Sign up to request clarification or add additional context in comments.

Comments

1
std::find(some_array.begin(), some_array.end(), some_value) - some_array.begin()

should do the trick (not tested).

1 Comment

Yes, std::find returns an iterator - that's why I subtract some_array.begin() from it, to arrive at an index. You could use std::distance instead if you are so inclined.
1

Is it possible to do it using std::find?

Yes: with a std::array it's possible.

std::array<int, 10> some_array = { 89, 56, 78, 96, 4, 34, 77, 2, 48, 3};

auto idx =   std::find(some_array.cbegin(), some_array.cend(), 34)
           - some_array.cbegin();

std::cout << "The index of value passed is " << idx << std::endl;

With std::find() you get an iterator that, in the case of std::array (and std::vector) is a random access iterator and support the difference; so you can subtract the cbegin() iterator getting the index

But this doesn't mean that you can avoid a loop: the loop is inside std::find().

See also the answer from cigien that works also with containers not supporting random access iterators: you can use std::distance().

But, in case of a container supporting a non-random access iterator, I suppose that std::distance() can introduce a second loop. So, in this case, I suppose is better if you directly write a single loop.

4 Comments

@vsoftco - in case of a random access iterator (std::array and std::vector, at least) is guarantee, as far I know.
Not 100% sure. It's certainly true that the Distance type is ptrdiff_t, en.cppreference.com/w/cpp/iterator/iterator, but cannot find anywhere an operator- for random access iterators.
@vsoftco - well... I see that now is named "LegacyRandomAccessIterator". I suppose it's to avoid confusion with the new (C++20) std::random_access_iterator concept... C++ become more complicated with every passing version.
@vsoftco - anyway, from this page you can see that an std::array iterator has to be a "LegacyRandomAccessIterator" (before C++20) and from this page you can see that must support the difference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.