0

i have a vector of set and i want to do compare to get intersection of all these sets

#include<iostream>
#include<set>
#include<vector>
#include <algorithm> 
#include <string>
using namespace std;



int main() {

    
    vector<set<string>>data;


    string courses;
    int students = 0, numberOfCourses = 0;
    cin >> students;

    for (int i = 0; i < students; i++) {
        cin >> numberOfCourses;
        set<string>st;
        for (int j = 0; j < numberOfCourses; j++) {
            cin >> courses;
            st.insert(courses);
        }                                                       
        data.push_back(st);
    }



    
    


    
    

}

i searched but all answers about comparing two different vectors i want to compare same vector

1 Answers1

0

You seem to want to know the courses that have all students enrolled in.

When you do st.insert(courses); you know that a specific student has a given course.

So, if you add map<string, int> numberOfStudentsInCourse; and

st.insert(courses);
numberOfStudentsInCourse[courses]++;

Then you just need to check, at the end, which numberOfStudentsInCourse have their second member equal to the number of students.

I'm not going to write the full code for you :-).

Jeffrey
  • 11,063
  • 1
  • 21
  • 42