@fjaerj12

Почему не работает std::set_intersection?

Не работает std::set_intersection в данном примере.
Входные данные:
5
1 2 3 4 5
3
1 2 8
Выходные данные:
Должно вывести 1 2, но выводится только пустая строка. При отладке становится видно, что мультимножество result пустое.
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>

int main()
{
    int lengthFirst = 0, lengthSecond = 0;
    std::cout << "Input a length of the first multiset ";
    std::cin >> lengthFirst;

    int value = 0;
    std::multiset<int> firstMultiset;
    for (int i = 0; i < lengthFirst; i++)
    {
        std::cout << "Input a value ";
        std::cin >> value;
        firstMultiset.insert(value);
    }

    std::cout << "Input a length of the second multiset ";
    std::cin >> lengthSecond;

    std::multiset<int> secondMultiset;
    for (int i = 0; i < lengthSecond; i++)
    {
        std::cout << "Input a value ";
        std::cin >> value;
        secondMultiset.insert(value);
    }

    std::multiset<int>result;
    std::multiset<int>::iterator iter;
    
    std::set_intersection(firstMultiset.begin(), firstMultiset.begin(), 
                              secondMultiset.begin(), secondMultiset.end(), std::inserter(result, result.begin()));

    for (iter = result.begin(); iter != result.end(); iter++) std::cout << *iter << " ";
}
  • Вопрос задан
  • 149 просмотров
Решения вопроса 1
Alexandroppolus
@Alexandroppolus
кодир
в твоем коде: std::set_intersection(firstMultiset.begin(), firstMultiset.begin()

должно быть: std::set_intersection(firstMultiset.begin(), firstMultiset. end()
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы