判断两个vector数组是否相等是可以直接使用==或者!=的

#include

using namespace std;

vector 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"<

else if(vt1

else if(vt1>vt2) cout<<"vt1>vt2"<

return 0;

}

因为vector内部都进行了相关运算符的重载,还可以进行比较大小

template< class T, class Alloc >

bool operator==( const vector& lhs,

const vector& rhs );

template< class T, class Alloc >

bool operator!=( const vector& lhs,

const vector& rhs );

template< class T, class Alloc >

bool operator<( const vector& lhs,

const vector& rhs );

template< class T, class Alloc >

bool operator<=( const vector& lhs,

const vector& rhs );

template< class T, class Alloc >

bool operator>( const vector& lhs,

const vector& rhs );

template< class T, class Alloc >

bool operator>=( const vector& lhs,

const vector& rhs );

下面这道简单搜索题就用到了这个性质,浅看一下吧

【问题描述】给定一个n个整数的集合X={x1,x2,…xn}(X中可能包含重复元素)和整数y,找出和等于y的X的子集Y。例如说,如果X={10,30,20,60,40,50},和y=60,则有4种不同的解,他们分别是{10,20,30},{10,50}{20,40},{60}。 【输入形式】输入的第1行包含两个整数n和y,分别表示集合X的长度和目标整数y。接下来1行包含n个整数(整数之间以空格分割),表示X中的n个元素。 【输出形式】输出1行包含1个整数,表示不同的解决方案(不能包含重复的方案数)。 【样例输入】

6 60

10 30 20 60 40 50 【样例输出】

4 代码:

#include

using namespace std;

const int N=1e3+10;

int n,y,ans;

int a[N],b[N];

vectorvt[N];

set>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

{

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<

return 0;

}

//测试

//5 30

//10 10 10 10 20

//结果:2

推荐链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: