1 c++ vector object stdvector visual-c++
我有一个User类,私人用户可以个性化用户的个人资料,例如姓名,年龄,收藏夹.我想比较两个给定用户的收藏夹并输出常见的收藏夹.因为我将使用向量,我的问题是如何将用户分组到vector(user1,user2),这样我就可以比较任何给定成员的收藏并输出结果.这就是我到目前为止所拥有的
using namespace std;
vector<string> user;
int adduser();
int adduser()
{
ofstream thefile;
thefile.open("users.txt", ios::app);
string firstname;
string lastname;
int age;
string favourites;
cout<<"Enter your Name: ";
cin>>name;
thefile << name << ' ';
cout<<"Enter your age: ";
cin>>age;
thefile << age << ",";
cout<<"Enter your favourites: ";
cin>>favourites;
thefile << favourites<< ",";
thefile.close();
cout<<" Completed."<<endl;
return 0;
}
common favourites()
{
//how do make it so i have something like user1 and user2
//which are both vectors so when i enter the name of
//user1 and user2 i can compare thier favourites and output the common ones
}
Run Code Online (Sandbox Code Playgroud)
您可以使用<algorithms>库,假设您可以对向量进行排序:
std::sort(user1.begin(), user1.end());
std::sort(user2.begin(), user2.end());
std::vector<string> common;
std::set_intersection(user1.begin(), user1.end(), user2.begin(), user2.end(), std::back_inserter(common));
Run Code Online (Sandbox Code Playgroud)