vwyf0305
vwyf0305
2019-01-25 16:05

我C++这个程序一直提示溢出不知道为什么?(新人,初学STL)

  • c++

题目是求集合的交集并集差集,我按照cppreference的来但一直报错,不知道怎么回事。

#include<cstdio>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
inline void insertl(vector<int> &a, int n)
{
    int num;
    for (int i = 0; i != n; i++)
    {
        cin >> num;
        a.push_back(num);
    }

}
inline void show(vector<int> &a)
{
    for (vector<int>::iterator itr = a.begin(); itr != a.end(); itr++)
    {
        cout << *itr<< " ";
    }
    cout << endl;
}
int main() {
    int an, bn;//集合A,B中的元素个数
    int num;
    vector<int> aggregate_A;//集合A
    vector<int> aggregate_B;//集合B
    cin >> an;
    insertl(aggregate_A, an);//将元素插入集合A
    cin >> bn;
    insertl(aggregate_B, bn);//将元素插入集合B
    vector<int> result_1(1000);//交集
    vector<int> ::iterator ret_1;
    ret_1 = set_intersection(aggregate_A.begin(), aggregate_A.end(), aggregate_B.begin(), aggregate_B.end(), result_1.begin());
    result_1.resize(ret_1 - result_1.begin());
    vector<int>result_2(1000);//并集
    vector<int> ::iterator ret_2;
    ret_2 = set_union(aggregate_A.begin(), aggregate_A.end(), aggregate_B.begin(), aggregate_B.end(), result_2.begin());
    result_2.resize(ret_2 - result_2.begin());
    vector<int>result_3(1000);
    vector<int>::iterator ret_3;
    ret_3 = set_difference(aggregate_A.begin(), aggregate_A.end(), aggregate_B.begin(), aggregate_B.end(), result_3.begin());
    result_3.resize(ret_3 - result_3.begin());
    show(result_1);
    show(result_2);
    show(result_3);

    system("pause");
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答