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; } } } 
9
  • Do you know how to use std::search? Because this is not what std::find is for. Commented Jan 27, 2020 at 0:11
  • @SamVarshavchik No, I am unfamiliar with std::search. Commented Jan 27, 2020 at 0:27
  • Well, I guess it's time to become familiar with it, because that's how this should be done. Commented Jan 27, 2020 at 0: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. Commented Jan 27, 2020 at 1: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? Commented Jan 27, 2020 at 1:30

1 Answer 1

1

You are likely looking for

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

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.