std::find() is the droid you are looking for
I see that you have looked up the documentation for the function (good job!), but you were not careful when you read it.
std::find() does
not return a string -- it returns an
iterator.
For an array, the iterator will be a pointer to the element type. Thus:
string* hi = std::find (textxOfHi, textsOfHi+8, "hi");
I recommend you use the
std::begin() and
std::end() functions; that allows you to change your table at any time without having to hunt down any magic numbers (like 8):
string* hi = std::find (std::begin(textxOfHi), std::end(textsOfHi), "hi");
You can also use
auto to not worry about the actual type of the iterator, for the same reason you should use begin() and end():
auto hi = std::find (std::begin(textxOfHi), std::end(textsOfHi), "hi");
Once you have that iterator, you can check to see whether or not it is valid:
1 2
|
if (hi != std::end(textsOfHi))
std::cout << "Yeah! Found it!\n";
|
Get the last argument in (for good)
Sorry I confused you. I used a
lambda -- a function without a name. Many Standard Library functions take as argument a function that compares two elements in a sequence. The following are equivalent:
1 2 3 4 5 6 7 8 9 10 11
|
// here is a NAMED FUNCTION that compares two characters without case sensitivity
bool compare_ci( char a, char b )
{
return std::tolower( a ) == std::tolower( b );
}
int main()
{
std::string one = "Hello";
std::string two = "hELLO";
bool ok = std::equal( one.begin(), one.end(), two.begin(), compare_ci );
|
1 2 3 4 5 6 7 8 9 10 11
|
int main()
{
// here is a NAMED LAMBDA that compares two characters without case sensitivity
auto compare_ci = []( char a, char b ) -> bool
{
return std::tolower( a ) == std::tolower( b );
};
std::string one = "Hello";
std::string two = "hELLO";
bool ok = std::equal( one.begin(), one.end(), two.begin(), compare_ci );
|
1 2 3 4 5 6 7 8 9 10 11 12
|
int main()
{
std::string one = "Hello";
std::string two = "hELLO";
bool ok = std::equal( one.begin(), one.end(), two.begin(),
// Here is an UNNAMED LAMBDA that compares two characters without case sensitivity
[]( char a, char b ) -> bool
{
return std::tolower( a ) == std::tolower( b );
}
);
}
|
You can recognize the lambda by it's syntax:
[ stuff ] ( arguments ) -> result_type { body }
For comparison, here's a named function:
result_type name( arguments ) { body }
If you do not specify the result_type for a lambda, the compiler will endeavor to guess. (It's pretty good at that.)
Hope this helps.