向量的大小是否有可能为 1 但其中的元素数量为零?

Is there any possibility that the size of the vector is 1 but with zero number of elements in it?

本文关键字:元素 是否 有可能 向量      更新时间:2023-10-16

在下面的代码中,我有 4 个向量abv1v2

在计算了 a 和 b、b 和 a 的集合差之后,我将它们的重用分别存储在 v1 和 v2 向量中。

  • 向量 'a' 具有元素 {'a'、'b'、'c'} 和
  • 向量 'b' 具有元素 {'a','b'}。
  • 现在 v1 包含大小为 1 的"c",但 v2 不包含任何大小为 1。 这怎么可能? 请注意,向量 a 和 b 包含集合的元素!!

这是我的代码:

#include <bits/stdc++.h>
#define REP(i,x,y) for(auto i=x;i!=y;i++)
using namespace std;
int t;
string s1,s2;
bool flag;
int main() 
{
cin>>t;
while(t--)
{
flag=false;
set<char> a,b;  multiset<char> A,B;
cin>>s1>>s2;
REP(i,0,s1.length())
{
A.insert(s1[i]);
a.insert(s1[i]);
B.insert(s2[i]);
b.insert(s2[i]);
}
vector<char> v1,v2;
set_difference(a.begin(),a.end(),b.begin(),b.end(),inserter(v1,v1.begin()));
set_difference(b.begin(),b.end(),a.begin(),a.end(),inserter(v2,v2.begin()));
cout<<v1.size()<<" "<<v2.size()<<"n";
REP(i,v1.begin(),v1.end())
cout<<*i<<" ";
cout<<"n";
REP(i,v2.begin(),v2.end())
cout<<*i<<" ";
cout<<"n";
}
return 0;

}

输入: 3 阿巴克 血型 阿坝 CDE 血型 光盘

输出:

1 1
c 
2 3
a b 
c d e 
2 2
a b 
c d 

set_difference需要排序的容器。

template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);

构造从结果所指向的位置开始的排序区域 使用排序范围 [first1,last1( 的集合差 相对于排序范围 [前 2,最后 2(。


vector<int> a={'a','b','a','c'}, b ={'a','b'}, v1,v2;
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
set_difference(a.begin(),a.end(),b.begin(),b.end(),inserter(v1,v1.begin()));
set_difference(b.begin(),b.end(),a.begin(),a.end(),inserter(v2,v2.begin()));

std::cout << "Vector v1: (" << v1.size() << ")" << std::endl;
for(auto it:v1)
{
std::cout << it << std::endl;
}
std::cout << "Vector v2: (" << v2.size() << ")" << std::endl;
for(auto it:v2)
{
std::cout << it << std::endl;
}

输出

Vector v1: (2)
97
99
Vector v2: (0)

据我所知,没有这种可能性。 问题出在代码本身!! 在提供输入时,我应该将字符串作为大小相等的字符串提供。 这就是我出错的地方,因此我没有在集合中插入任何东西。 谢谢大家有宝贵的时间解决我的问题。