Search code examples
c++setmultimap

How to Find a Substring in a Particular String in a Set


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;
            }
        }
    }   

Solution

  • You are likely looking for

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

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