操作字符串的艺术:提升C++开发效率
1. 改变大小写和不区分大小写的比较
在日常编程中,处理字符串是一项常见的任务。特别是在需要进行大小写不敏感的比较时,使用标准库函数可能会显得笨拙。幸运的是,Boost库提供了多种简便的方法来解决这些问题。
如何操作…
以下是几种进行不区分大小写的比较的方法:
-
使用
boost::iequals
:
cpp #include<boost/algorithm/string/predicate.hpp> boost::iequals(str1, str2);
-
使用Boost谓词和STL方法 :
cpp #include<boost/algorithm/string/compare.hpp> #include<algorithm> str1.size() == str2.size() && std::equal(str1.begin(), str1.end(), str2.begin(), boost::is_iequal());
-
创建两个字符串的小写副本 :
cpp #include<boost/algorithm/string/case_conv.hpp> std::string str1_low = boos