Características pequeñas pero importantes

Invitamos a los futuros alumnos del curso "C ++ Developer. Professional" a participar en la lección abierta "Backend en C ++ moderno". Mientras tanto, compartamos la traducción tradicional del material.
















C++20 , std::map



, std::set



std::string



, . , , . , .





Map set

map



— , . C++17? :





std::map<int, std::string> m{ {1, "one"}, {2, "two"}, {3, "three"} };
 
if (m.find(1) != m.end())
{
   std::cout << "key found!\n";
}
      
      



, . contains()



, map



, , map



. C++20 , .. std::map



contains()



.





std::map<int, std::string> m{ {1, "one"}, {2, "two"}, {3, "three"} };
 
if (m.contains(1))
{
   std::cout << "key found!\n";
}
      
      



std::set







std::set<int> s{ 1, 2, 3 };
if (s.contains(1))
{
   std::cout << "key found!\n";
}
      
      



, C++20 contains()



​​ :





  • std::map





  • std::multimap





  • std::unordered_map





  • std::unordered_multimap





  • std::set





  • std::multiset





  • std::unordered_set





  • std::unordered_multiset





. , . C++17:





std::string text{"The quick brown fox jumps over the lazy dog"};
 
if (text.find("fox") != std::string::npos)
{
   std::cout << "fox found!\n";
}
      
      



, , — . :





if (text.find("The quick") == 0)
{
   std::cout << "right start\n";
}
      
      



. :





bool ends_with(std::string const & text, std::string const & substr)
{
   if (substr.size() > text.size()) return false;
   return std::equal(text.begin() + text.size() - substr.size(), text.end(), substr.begin());
}
      
      



:





if (ends_with(text, "lazy dog"))
{
   std::cout << "right end\n";
}
      
      



(: )





C++20 , std::basic_string



std::basic_string_view



: starts_with() ends_with()





if (text.starts_with("The quick"))
{
   std::cout << "right start\n";
}
 
if(text.ends_with("lazy dog"))
{
   std::cout << "right end\n";
}
      
      



C++20 : , . ISO C++, C++23 (P1679). :





if (text.contains("fox"))
{
   std::cout << "fox found!\n";
}
      
      



.





, . , . , , . contains()



, startwith()



endwith()



, , :





bool contains_ci(std::string const & text, std::string const & substr)
{
   if (substr.length() > text.length()) return false;
 
   auto it = std::search(
      text.begin(), text.end(),
      substr.begin(), substr.end(),
      [](char ch1, char ch2) { 
         return std::toupper(ch1) == std::toupper(ch2); });
 
   return it != text.end();
}
 
bool starts_with_ci(std::string const& text, std::string const& substr)
{
   if (substr.length() > text.length()) return false;
 
   auto it = std::search(
      text.begin(), text.begin() + substr.length(),
      substr.begin(), substr.end(),
      [](char ch1, char ch2) {
         return std::toupper(ch1) == std::toupper(ch2); });
 
   return it == text.begin();
}
 
bool ends_with_ci(std::string const& text, std::string const& substr)
{
   if (substr.length() > text.length()) return false;
 
   auto it = std::search(
      text.rbegin(), text.rbegin() + substr.length(),
      substr.rbegin(), substr.rend(),
      [](char ch1, char ch2) {
         return std::toupper(ch1) == std::toupper(ch2); });
 
   return it == text.rbegin();
}
      
      



:





if (contains_ci(text, "FOX"))
{
   std::cout << "fox found!\n";
}
 
if (starts_with_ci(text, "THE QUICK"))
{
   std::cout << "right start\n";
}
 
if (ends_with_ci(text, "LAZY DOG"))
{
   std::cout << "right end\n";
}
      
      




"C++ Developer. Professional"



"Backend ++"






:

  • C++17
















All Articles