比较两个字符串,用逗号分隔值 [英] comparing two strings with comma separated values
本文介绍了比较两个字符串,用逗号分隔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以比较两个具有逗号分隔值的字符串吗?
Is there any way by which we can compare two strings having comma separates values?
为清楚起见:
string S1 = "a,b,c,d,e"
string S2 = "c,d"
string S3 = "a,b,e" //where string S3 is got by subtracting S2 from S1 (S1 - S2)
是否可以通过某些功能来做到这一点?
Is it possible to do this with some function?
推荐答案
如果我正确地得到了想要的东西,则希望获得两个字符串中都不存在的元素.
If I got what you want right, you want to get the elements that are not in both strings.
这就是我要怎么做:
- 使用
,
作为分隔符将a和b都分开. - 将结果存储在两组中(例如,
std :: set
) - 计算这两个集合的差(例如,使用
std :: set_difference
) - 通过使用
,
将每个元素粘合在一起,将结果集转换为字符串
- Split both a and b using
,
as a separator. - Store the result in two sets (
std::set
for instance) - Compute the difference of these two sets (for example, using
std::set_difference
) - Convert the resulting set to a string by gluing each element together using
,
这篇关于比较两个字符串,用逗号分隔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文