我想检查一个字符串是否是严格的另一个字符串的子集。 对于为此我用boost::contains,我比较字符串的大小,如下所示:如何检查字符串是否是另一个字符串的一个子集
#include <boost/algorithm/string.hpp>
#include <iostream>
using namespace std;
using namespace boost::algorithm;
int main()
{
string str1 = "abc news";
string str2 = "abc";
//strim strings using boost
trim(str1);
trim(str2);
//if str2 is a subset of str1 and its size is less than the size of str1 then it is strictly contained in str1
if(contains(str1,str2) && (str2.size() < str1.size()))
{
cout <<"contains" << end;
}
return 0;
}
有没有更好的办法来解决这个问题呢?而不是比较字符串的大小?
例
- ABC是ABC新闻
- ABC的适当子集并不的的适当子集ABC
为什么不直接使用'的std :: string :: find'? – Lingxi
是否与正确的子集类似? –
我会反转测试(即在*调用'contains()'之前检查大小*),除此之外,我认为你不能做得更好。 – Nim