c++ - 表达式: Sequence not ordered

标签 c++ crash std intersection

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

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/

相关文章:

java - 如何在java中执行 "an exe with return value"?

c++ - 循环运行的函数超出了它的需要

C++ 无法将派生类转换为基类

ios - Apple 拒绝 - 无法重现崩溃

c++ - 标准线程分离

C++:std::copy esque函数,用于将一个 vector 的值映射到另一个 vector

c++ - constexpr 构造函数和函数的文字类编译错误(不同 vc、g++)

ios - 核心数据迁移失败

c++ - libcurl curl_easy_perform 崩溃(段错误)c++

c++ - 使用 VC141 将 high_resolution_clock::time_point 转换为 time_t