0

I'm really new to coding. In my program, I'm trying to assign proper prefixes to countries. For example: putting in "Philippines" gives out "les Philippines".

Anyway, I'm trying to find if a country is in my "plain" list of exceptions of countries that don't need a prefix. So if you put in "israel" you only get "Israel"

string country;
string prefix;
string plain = "Isreal, Madagascar, Sri Lanka, Singapore, Monaco, Cuba, Cyprus";
string masculine = "Belize, Camboge, Mexique, Mozambique, Costa Rica, Zimbabwe, Honduras";

I'm assuming I need something like this:

if (plain.find(country))
    {
        prefix = "";
    }

but it's not working? Can someone explain how it works? I could do a long if statement, but I'd rather avoid that.

Michele
  • 33
  • 6
  • 5
    `if (plain.find(country) != std::string::npos)` – drescherjm Feb 17 '21 at 03:11
  • 2
    You'd be better off storing this list in a `std::unordered_set` and searching that. `std::unordered_set plain { "Israel", "Madagascar" /* , etc... */ }; if (plain.contains(country)) ...` (C++20) or simply `if (plain.find(country) != plain.end()) ...` – paddy Feb 17 '21 at 03:14
  • It's better to use some sort of container than storing strings as a subset of another string, but someone who's brand new shouldn't be worried about `std::unordered_set` vs `std::vector`. – Gyross Feb 17 '21 at 03:22
  • @paddy why unordered instead of a normal set? – M.M Feb 17 '21 at 03:31
  • @M.M std::unordered_set is more effective than std::set. The disadvantage is that it is ... not ordered. In general you can replace any std::unordered_set by std::set and the functionality will be the same (but obviously you cannot replace it the opposite way); still you may need to provide different custom operator< vs. operator==&&hash. – Jan Kratochvil Feb 17 '21 at 05:02
  • @M.M it's a habit born from personal preference, and usually because when I'm using such structures for lookups I tend to be chasing performance. If I don't require the data to be ordered, I use `unordered_set`. If I require ordering, I use `set`. But sure, `set` would work just fine too. I maintain that either of these is preferable to searching stuff in a freeform string. Consider partial string matches like `Niger` matching `Nigeria` or `Dominica` matching `Dominican Republic`. Just using countries as an example here, where having a list in a single string would break. – paddy Feb 17 '21 at 05:16

1 Answers1

0

If you need to understand what any function is doing or if anything goes wrong with it , I'd recommend you to look for its documentation. One of the popular website for docs of cpp is cplusplus.com. In your case as suggested in the comments you just need to change your if condition to if (plain.find(country) != std::string::npos) because std::string::find returns std::string::npos if no matches were found.

  • "*One of the popular website for docs of cpp is cplusplus.com*" - actually, that site is generally considered to be [not a good site to use](https://stackoverflow.com/questions/6520052/), it is full of inaccuracies and misinformation. The *more popular* and *more accurate* site is [cppreference.com](https://cppreference.com) instead. – Remy Lebeau Feb 17 '21 at 08:47
  • @RemyLebeau I didn't know that, thanks for correcting me. – Dhavalsinh raj Feb 28 '21 at 11:30