向量的大小是否有可能为 1 但其中的元素数量为零?
Is there any possibility that the size of the vector is 1 but with zero number of elements in it?
在下面的代码中,我有 4 个向量a
、b
、v1
、v2
。
在计算了 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)
据我所知,没有这种可能性。 问题出在代码本身!! 在提供输入时,我应该将字符串作为大小相等的字符串提供。 这就是我出错的地方,因此我没有在集合中插入任何东西。 谢谢大家有宝贵的时间解决我的问题。
相关文章:
- C++默认情况下,指针类型数组的元素是否保证初始化为 nullptr?
- 检查 2D 网格的某个元素是否与另一个元素共享对角线、水平线或垂直线
- 如何知道地图中的最后一个元素是否被删除?
- 检查 TinyXML 中的元素是否存在
- 使用 at() 访问 std::map 元素是否比运算符 [] 慢?
- 在迭代期间添加到 std::unordered_set(或 unordered_map)中的元素是否会在迭代期间访问?
- 使用 Rcpp 加速替换迭代算法中的列表和向量元素是否合法?
- 通过引用访问矢量元素是否会降低C++的空间复杂性?
- 检查HTML 5视频元素是否正在获取帧
- 在c++中,如何检查一个数组的元素是否也是另一个数组中的元素
- 是否有更有效的方法来检查元素是否在给定的区间内
- 如何检查数组的 3 个元素是否具有相同的值
- 整数指针数组的元素是否可以指向整数数组?
- 在 new 关键字中,由默认构造函数初始化的类中的元素是否也使用 new 关键字在C++?
- 写入多维矢量子级中的元素是否安全?
- 使用容器中元素的别名删除带有 std::list::remove 的元素是否正确?
- 如何确定向量的元素是否交替为符号?
- 如何查看矢量中的所有元素是否相同?
- 从标准容器中移动元素是否合法?
- 如何检查 std::vector<std::string> 的元素是否以某个子字符串开头?