将两个字符串与逗号分隔值进行比较 [英] comparing two strings with comma seperated values

查看:222
本文介绍了将两个字符串与逗号分隔值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以比较两个有逗号分隔值的字符串?



清楚的说明:

  string S1 =a,b,c,d,e
string S2 =c,d
string S3 =a,b ,e//其中通过从S1减去S2得到的字符串S3(S1-S2)

Is它可以用一些功能来做到这一点?

解决方案

如果我得到你想要的东西,你想得到的元素,



以下是我的操作方式:


  1. 拆分 c>

  2. 将结果存储在两个集合中( std ::例如 c c
  3. 计算这两个集合的差异(例如,使用 std :: set_difference
  4. 使用

  5. 将每个元素粘贴到一起, ol>

    Is there any way by which we can compare two strings having comma seperates values?

    To make it clear:

    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.

    Here's how I would do it:

    1. Split both a and b using , as a separator.
    2. Store the result in two sets (std::set for instance)
    3. Compute the difference of these two sets (for example, using std::set_difference)
    4. Convert the resulting set to a string by gluing each element together using ,

    这篇关于将两个字符串与逗号分隔值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆