我正在尝试找到两个字符串之间的交集。我正在使用以下代码:
std::string a = "asd", b = "afd";
std::string intersect;
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersect));
编译成功,但运行程序后立即崩溃,并出现以下错误:
有什么建议导致这个问题吗?
最佳答案
您应该排序a
和b
首先将它们传递给 std::set_intersection()
:
template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_intersection( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first ); (1) template< class InputIt1, class InputIt2, class OutputIt, class Compare > OutputIt set_intersection( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first, Compare comp ); (2)
Constructs a sorted range beginning at
d_first
consisting of elements that are found in both sorted ranges[first1, last1)
and[first2, last2)
. The first version expects both input ranges to be sorted withoperator<
, the second version expects them to be sorted with the given comparison functioncomp
.
所以,添加
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
关于c++ - 表达式: Sequence not ordered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23318981/