gpt4 book ai didi

c++ - 表达式: Sequence not ordered

转载 作者:行者123 更新时间:2023-12-01 23:08:38 28 4
gpt4 key购买 nike

我正在尝试找到两个字符串之间的交集。我正在使用以下代码:

std::string a = "asd", b = "afd";

std::string intersect;
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersect));

编译成功,但运行程序后立即崩溃,并出现以下错误:

enter image description here

有什么建议导致这个问题吗?

最佳答案

您应该排序ab首先将它们传递给 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 with operator<, the second version expects them to be sorted with the given comparison function comp.

所以,添加

std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());

关于c++ - 表达式: Sequence not ordered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23318981/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com