交叉点代码,用于打印两个数组(过帐列表)的交叉点
intersection code that prints the intersection of two arrays ( posting lists )
我写这段代码是为了使用dev c打印两个数组(发布列表)的交集++当我运行程序时,没有打印任何内容的问题你能帮忙吗
我需要知道问题出在哪里如果我想用cout而不是printf呢?
#include <iostream>
#include <stdio.h>
using namespace std;
// Intersecting two postings lists (a “merge” algorithm)
// and I will assume that the two posting listst are sorted Ascendingly
// I will suppose that the first posting list is an array wich
// have n elements I wil name it fistPost
// I will suppose that the second posting list is an array wich have
// m elements I will name it secondPost
int main()
{
int firstPost[] ={3,5,7,8,13,15,30,34};
int secondPost[]={1,5,7,9,11,15,20,34,35};
int i,j=0;
int n = sizeof(firstPost)/sizeof(firstPost[0]);
int m = sizeof(secondPost)/sizeof(secondPost[0]);
while(i<n && j<m)
{
if (firstPost[i]<secondPost[j])
i++;
else if (firstPost[i]>secondPost[j])
j++;
else if (firstPost[i]=secondPost[j])
{
printf ("%i", secondPost[j++]);
i++;
}
}
system("PAUSE");
return 0;
}
您还没有将'i'初始化为0,因此它接受了一些垃圾值,并且没有执行while循环。
改变int i,j=0;到int i=0,j=0;
更改此printf ("%i", secondPost[j++]);
至
printf ("%d", secondPost[j++]);
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '