简体   繁体   中英

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++). 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. STL is quite verbose because it is based on iterators instead of ranges which essentially are pairs of iterators. With Boost.Range, you can write

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. The union of two strings is not a common operation, it's probably used more in homework than anywhere else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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