判斷兩個(gè)vector數(shù)組是否相等是可以直接使用==或者!=的
#include<bits/stdc++.h>
using namespace std;
vector<int> vt1,vt2;
int main()
{
for(int i=1;i<=4;i++)
{
vt1.push_back(i);
vt2.push_back(i);
}
vt1.push_back(5);
if(vt1==vt2) cout<<"vt1==vt2"<<endl;
else if(vt1<vt2) cout<<"vt1<vt2"<<endl;
else if(vt1>vt2) cout<<"vt1>vt2"<<endl;
return 0;
}
因?yàn)関ector內(nèi)部都進(jìn)行了相關(guān)運(yùn)算符的重載,還可以進(jìn)行比較大小
template< class T, class Alloc >
bool operator==( const vector<T,Alloc>& lhs,
const vector<T,Alloc>& rhs );
template< class T, class Alloc >
bool operator!=( const vector<T,Alloc>& lhs,
const vector<T,Alloc>& rhs );
template< class T, class Alloc >
bool operator<( const vector<T,Alloc>& lhs,
const vector<T,Alloc>& rhs );
template< class T, class Alloc >
bool operator<=( const vector<T,Alloc>& lhs,
const vector<T,Alloc>& rhs );
template< class T, class Alloc >
bool operator>( const vector<T,Alloc>& lhs,
const vector<T,Alloc>& rhs );
template< class T, class Alloc >
bool operator>=( const vector<T,Alloc>& lhs,
const vector<T,Alloc>& rhs );
下面這道簡單搜索題就用到了這個(gè)性質(zhì),淺看一下吧
【問題描述】給定一個(gè)n個(gè)整數(shù)的集合X={x1,x2,…xn}(X中可能包含重復(fù)元素)和整數(shù)y,找出和等于y的X的子集Y。例如說,如果X={10,30,20,60,40,50},和y=60,則有4種不同的解,他們分別是{10,20,30},{10,50}{20,40},{60}。
【輸入形式】輸入的第1行包含兩個(gè)整數(shù)n和y,分別表示集合X的長度和目標(biāo)整數(shù)y。接下來1行包含n個(gè)整數(shù)(整數(shù)之間以空格分割),表示X中的n個(gè)元素。
【輸出形式】輸出1行包含1個(gè)整數(shù),表示不同的解決方案(不能包含重復(fù)的方案數(shù))。
【樣例輸入】
6 60
10 30 20 60 40 50
【樣例輸出】文章來源:http://www.zghlxwxcb.cn/news/detail-525712.html
4
代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-525712.html
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,y,ans;
int a[N],b[N];
vector<int>vt[N];
set<vector<int>>st;
void dfs(int x,int k,int sum)
{
//剪枝操作省略(前綴和剪枝)
if(sum>y) return;
b[k]=a[x];
if(sum==y)
{
ans++;
for(int i=1;i<=k;i++)
vt[ans].push_back(b[i]);
st.insert(vt[ans]);
return;
}
if(sum<y)
{
for(int i=x+1;i<=n;i++)
dfs(i,k+1,sum+a[i]);
}
}
int main()
{
cin>>n>>y;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1);
dfs(0,1,0);
cout<<st.size()<<endl;
return 0;
}
//測試
//5 30
//10 10 10 10 20
//結(jié)果:2
到了這里,關(guān)于判斷兩個(gè)vector數(shù)組是否相等的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!