C++关于vector的问题
问题要求是判断两个vector在不考虑重复元素和顺序的情况下是否相等,我想先去重从小到大排列再分辨于是写了如下的程序,但是却没有办法排列,请问这是为什么?#include...
问题要求是判断两个vector在不考虑重复元素和顺序的情况下是否相等,我想先去重从小到大排列再分辨于是写了如下的程序,但是却没有办法排列,请问这是为什么?#include"stdafx.h"
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
bool same_vec(vector<int> A, vector<int> B);
int S; //create a variable for cin
vector<int> A, B; // create vector A,B
cout << "Please input vector A, and ended by a character" << endl; // give a hint to stop reading
while (cin >> S); // give value to variable
A.push_back(S);
sort(A.begin(), A.end());
A.erase(unique(A.begin(), A.end()), A.end()); //delete the repeated elements
cin.clear(); // clear for a new loop.
cin.ignore();
sort(A.begin(), A.end());
cout << "Please input vector B, and ended by a character" << endl;
while (cin >> S);
B.push_back(S);
sort(B.begin(), B.end());
B.erase(unique(B.begin(), B.end()), B.end());
cin.clear(); // same as vector A.
if (A==B) // Test two vectors.
cout << "vector A,B are identical";
else
cout << "vector A,B are not identical";
return 0;
} 展开
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
bool same_vec(vector<int> A, vector<int> B);
int S; //create a variable for cin
vector<int> A, B; // create vector A,B
cout << "Please input vector A, and ended by a character" << endl; // give a hint to stop reading
while (cin >> S); // give value to variable
A.push_back(S);
sort(A.begin(), A.end());
A.erase(unique(A.begin(), A.end()), A.end()); //delete the repeated elements
cin.clear(); // clear for a new loop.
cin.ignore();
sort(A.begin(), A.end());
cout << "Please input vector B, and ended by a character" << endl;
while (cin >> S);
B.push_back(S);
sort(B.begin(), B.end());
B.erase(unique(B.begin(), B.end()), B.end());
cin.clear(); // same as vector A.
if (A==B) // Test two vectors.
cout << "vector A,B are identical";
else
cout << "vector A,B are not identical";
return 0;
} 展开
展开全部
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
//bool same_vec(vector<int> A, vector<int> B);
int S; //create a variable for cin
vector<int> A, B; // create vector A,B
cout << "Please input vector A, and ended by a character" << endl; // give a hint to stop reading
while (cin >> S) // give value to variable
A.push_back(S);
sort(A.begin(), A.end());
A.erase(unique(A.begin(), A.end()), A.end()); //delete the repeated elements
cin.clear(); // clear for a new loop.
cin.ignore();
sort(A.begin(), A.end());
cout << "Please input vector B, and ended by a character" << endl;
while (cin >> S)
B.push_back(S);
sort(B.begin(), B.end());
B.erase(unique(B.begin(), B.end()), B.end());
cin.clear(); // same as vector A.
cin.ignore();
if (A == B) // Test two vectors.
cout << "vector A,B are identical";
else
cout << "vector A,B are not identical";
cin.get();
cin.get();
return 0;
}
你的习惯不好,要注意使用{},你的while循环都出了这个问题,你多写了“;”,如果使用{}就不太可能这样,然后就是底下少了一句:cin.ignore();为了看到结果,我增加了
cin.get();
cin.get();
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
//bool same_vec(vector<int> A, vector<int> B);
int S; //create a variable for cin
vector<int> A, B; // create vector A,B
cout << "Please input vector A, and ended by a character" << endl; // give a hint to stop reading
while (cin >> S) // give value to variable
A.push_back(S);
sort(A.begin(), A.end());
A.erase(unique(A.begin(), A.end()), A.end()); //delete the repeated elements
cin.clear(); // clear for a new loop.
cin.ignore();
sort(A.begin(), A.end());
cout << "Please input vector B, and ended by a character" << endl;
while (cin >> S)
B.push_back(S);
sort(B.begin(), B.end());
B.erase(unique(B.begin(), B.end()), B.end());
cin.clear(); // same as vector A.
cin.ignore();
if (A == B) // Test two vectors.
cout << "vector A,B are identical";
else
cout << "vector A,B are not identical";
cin.get();
cin.get();
return 0;
}
你的习惯不好,要注意使用{},你的while循环都出了这个问题,你多写了“;”,如果使用{}就不太可能这样,然后就是底下少了一句:cin.ignore();为了看到结果,我增加了
cin.get();
cin.get();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询