0

How do you find the substring within a string in a set? For example, if I enter "Ville," then Louisville, Gainesville, and Muellerville are found? I have tried the following code

for(string const& search : cities)
{
if(find(search.begin(), search.end(), str) != std::string::npos)
{
string y = search;
employees.emplace_back(y);

,but I cannot figure out what is wrong with my syntax. This code is used in the following project (Project Code)

EDIT: My problem was simple and was fixed with using .begin() and .end() to iterate over the multimap name_address and finding each name with .substr. I also used a multimap instead of a set. I found the syntax easier and got it to work.

    for(auto it = name_address.begin(); it != name_address.end(); ++it)
    {

        for(int i = 0; i < it->first.length(); ++i)
        {   
            string tmpstr3 = it->first.substr(0 + i, str.length());
            if(str == tmpstr3)
            {
               employees.insert(it->second);
               break;
            }
        }
    }   
JackLalane1
  • 177
  • 10
  • Do you know how to use `std::search`? Because this is not what `std::find` is for. – Sam Varshavchik Jan 27 '20 at 00:11
  • @SamVarshavchik No, I am unfamiliar with std::search. – JackLalane1 Jan 27 '20 at 00:27
  • Well, I guess it's time to become familiar with it, because that's how this should be done. – Sam Varshavchik Jan 27 '20 at 00:29
  • @SamVarshavchik Maybe, I am reading the documentation wrong, but it says std::search returns the first element found with the string in it. I need all elements. – JackLalane1 Jan 27 '20 at 01:28
  • Well, if it returns the iterator to the first element, and you know that you were searching for five characters, and you know that it returns the iterator to the first one, how difficult do you think it is to find where the remaining four are? – Sam Varshavchik Jan 27 '20 at 01:30
  • @SamVarshavchik I'm not sure what you mean. – JackLalane1 Jan 27 '20 at 01:42
  • I mean that all you need is an iterator to "the first element". After all, a string literal in C++, like `"hello world"` is just a pointer to its first character. Somehow, C++ is always able to find the rest of the literal string. So, once you get the iterator to the first element, that `std::search` returns, you automatically know where the remaining elements are, by definition. That's how C++ iterators work. Isn't C++ awesome? – Sam Varshavchik Jan 27 '20 at 01:44
  • @SamVarshavchik Okay, the element I am referring to is the element of the set and not each individual character of each string within the set. So would std::search work, for example, if I enter "Ville," then Louisville, Gainesville, and Muellerville are found or would it just get Louisville and not the others? – JackLalane1 Jan 27 '20 at 01:50
  • If you `std::search` the string "Louisville" for "Ville", you can use the `std::search` overload that takes an extra comparator closure, and supply one that does a case-insensitive comparison, so that a `std::search` of "Louisville" for "Ville" will return an iterator to 'v'. Like I said: isn't C++ awesome? – Sam Varshavchik Jan 27 '20 at 02:30

1 Answers1

1

You are likely looking for

if (search.find(str) != std::string::npos)

The std::find call you have shouldn't compile.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85