In C ++, how do I return the index of an array if the user has entered matching input arrays?

I wanted to know how can we get the index of an array if the array entered by the user matches the input array?

For example:

Input Array = [1,2,3,4] and the user entered Array = [2,3] than I should get the output as an index where both array matches are 1.

The guidance will be much appreciated.

+2


source to share


1 answer


Use the STL search algorithm that does exactly what you want: "The search () algorithm searches for items [start2, end2) in the range [start1, end1)." You will need to provide pointers to the beginning and end of the two arrays; you get the ending pointer to the array by adding its length to the start pointer.

It's better to use an STL vector to store your data instead of an array, and then you can just call vec.begin () and vec.end () to get the iterators you want.



Edit: To do this without std :: search, follow the example below for a link that shows how a search can be performed. If you do it C style, you will be using pointers (like int *), not ForwardIterator. The only tricky bit is the part outside of the loop where they define what limit should be set - this will turn into some pointer arithmetic.

+4


source







All Articles