#include
#include
using namespace std;
int main ()
{
std::ios::sync_with_stdio (false);
setiset1{ 1, 3, 5, 7, 9, 11 };
setiset2{ 1, 2, 3, 4, 5, 6, 8, 10 };
setsame;
auto p1 = iset1.begin();
auto p2 = iset2.begin();
auto ps = same.begin();
if (!iset1.size () || !iset2.size ()) { cerr << "empty set exist!" << endl; return -1; }
for (size_t i = 0; i < iset1.size () && i < iset2.size (); ++i) {
if (*p1 > *p2)++p2;
else if (*p1 < *p2)++p1;
else { same.insert(*p1) ; ++p1; ++p2; }
}
copy(same.begin(), same.end(), ostream_iterator(cout, " "));
cout << endl;
return 0;
}
找出两串有序数组中相同的元素, 并存入新数组中的目前最快方法
最新推荐文章于 2024-03-11 14:58:46 发布