简体   繁体   English

有没有更短的方法来获取两个字符串的交集和并集?

[英]Is there any shorter way of getting an intersection and union of two strings?

Is there any shorter way of getting an intersection and union of two strings? 有没有更短的方法来获取两个字符串的交集和并集? I was looking for how to get an intersection and union of strings and encountered this answer Intersection and union of two strings , however, it seems too long for this kind of simple operation, maybe it is because I used to python style (I recently started learning C++). 我一直在寻找如何获取字符串的交集和并集,并遇到此答案两个字符串的交集和并集 ,但是,对于这种简单的操作而言似乎太长了,也许是因为我曾经使用过python样式(我最近开始学习C ++)。 Anyway, I would be very grateful if someone shows me concise way of this. 无论如何,如果有人向我展示此方法,我将不胜感激。

You can reduce the verbosity by using a range library, eg Boost.Range. 您可以通过使用范围库(例如Boost.Range)来减少详细程度。 STL is quite verbose because it is based on iterators instead of ranges which essentially are pairs of iterators. STL非常冗长,因为它基于迭代器,而不是本质上是成对的迭代器的范围。 With Boost.Range, you can write 使用Boost.Range,您可以编写

std::string s1, s2;
std::string difference;
boost::set_difference( boost::sort(s1), boost::sort(s2), std::back_inserter(difference) );

As the comments already suggested, no. 正如评论所暗示的,不。

Essentially, the "verbosity" of the linked solution is because it's necessary to specify all those arguments in order to be generic. 本质上,链接解决方案的“冗长性”是因为必须指定所有这些参数才能通用。 Some common operations have less verbose specific variants (eg string::operator= is less verbose to use than std::equal ) but that covers common functions. 一些常见的操作具有较少的详细特定变体(例如,与std::equal相比, string::operator=使用较少冗长),但涵盖了常见的函数。 The union of two strings is not a common operation, it's probably used more in homework than anywhere else. 两个字符串的并集不是一个常见的操作,它在家庭作业中可能比在其他任何地方都使用更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM