ok....so i have code that will return whether or not the string im searching for is found. Which is fine and dandy, but i also need the array index, for where the match was found. (names are read from a txt into an array, then the array is searched with the following code)

************code****************
if ( std::binary_search(names, names + 4, search) )
cout << "Found at " << << endl;
else
cout << "Not found" << endl;

**************/code**************

so what this boils down to... caa i modify what i have to return and array index? or must i ditch this ?

You want to look at lower_bound. It has the same complexity as binary_search (and also needs a sorted list). But it returns an iterator.

correct me if I'm wrong, but this seems to be designed for ints.

in other words....i need to find another, less complicated way to do this. I need to search a string array for a string that was input, and either return the index where the string was found, or report that the search was unsuccessful. Right now, the input (being searched for) is stored as string search;

> correct me if I'm wrong, but this seems to be designed for ints.

No, it's for any type that has < defined (or you can pass in a comparison function).

The reason I mentioned lower_bound instead of simply find (not string's find but the algorithm find) is that your use of binary_search suggested you had a sorted array.

If your array is NOT sorted then you want find as it will work on a sorted or unsorted list; but it is less efficient than the binary search performed by lower_bound.

lower_bound is easy to use, as shown in the test code below.

// lower_bound returns an iterator to the greatest element not
// less than the search element. If the search element is greater
// than all elements, then one past the last element is returned
// (i.e., the end iterator).

#include <iostream>
#include <algorithm>

void test_lower_bound( std::string *searchIn,
                       size_t       searchInSize,
                       std::string  searchFor )
{
    std::string *it = std::lower_bound( searchIn,
                                        searchIn + searchInSize,
                                        searchFor );
    // We need to check not only that the returned itereator is
    // in range, but that the element it refers to actually is
    // equal to the one we were looking for.
    if( it - searchIn >= searchInSize || *it != searchFor )
        std::cout << searchFor << " not found: fits in at index "
                  << it - searchIn << '\n';
    else
        std::cout << searchFor << " found at index: "
                  << it - searchIn << '\n';
}

int main() {
    const int NamesSize = 4;
    std::string names[ NamesSize ] = {"b", "d", "f", "h"};

    // Test with strings "a" to "k"
    for ( char search[] = "a"; search[0] <= 'k'; ++search[0] )
        test_lower_bound( names, NamesSize, search );
}

i apologize for my lack of clarity.

i have 4 names stored in a txt file. They are read into a string array.
The array is not sorted.

i have a string search; that stores the input of the user.

search contains the string that needs to be found in the array of strings. The array is one dimensional, so i only need the program to provide what element of the array the string was found in, (providing it exists).

It looks like the find will work. i just need to read more about it, and gain a better understanding of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.