I have an array of strings, with each string being a name. I'm looking for a function that will find the index of a particular name in the array. I looked into Array::IndexOf, but my array is like this:

string names=new string[30];

and it wont work. I could make my own if needed, but it would be a lot better if there was a built in one. Thanks in advance

It is better for you to implement your own. As written in std::find, if value is not found, the last will be returned. In this case, when the last index is returned, you cannot tell if the last item matches OR it is not found in your list.

@chiwawa10 you have miss-understood how std::find works and should be used and in fact you only have to look at the example in the page you have linked to to find the error.

The end pointer should be a pointer to the first location just passed the end of the array so for an array of any type T and size N

T array[N] = {};
T searchValue;
// Initialise array

std::find(array, array+N, searchValue);

std::find will return array+N if the value is not found or a pointer to one of the array elements if it is.

Edited 6 Years Ago by Banfa: n/a

Perhaps you mean string [b]*[/b]names=new string[30]; , right? (note the '*' character before the variable name). It is possible to make it std::vector<string> and use the internal finding algorithms of STL.

This article has been dead for over six months. Start a new discussion instead.