调用合并函数时没有输出
No output when merge function is called
我对以下代码有疑问。有人可以解释一下。
using namespace std;
#define INT_SIZE 32
#define R 4
#define C 4
#define N 4
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
#include<stack>
#include<vector>
#include<algorithm>
struct interval{
int start;
int end;
};
bool compareInterval(interval i1, interval i2)
{
return (i1.start < i2.start)? true: false;
}
int merge(vector<interval>& a, int n)
{
stack<interval> s;
sort(a.begin(), a.end(), compareInterval);
s.push(a[0]);
int i=1;
interval temp;
while(i<n)
{
temp = s.top();
s.pop();
if(temp.end > a[i].start && a[i].end > temp.end)
{
temp.end = a[i].end;
s.push(temp);
}
else if(temp.end < a[i].start)
{
s.push(temp);
s.push(a[i]);
}
i++;
}
while(s.size())
{
temp = s.top();
cout << temp.start << " ";
cout << temp.end << "n";
s.pop();
}
return 0;
}
int main()
{
interval intvls[] = { {6,8}, {1,9}, {2,4}, {4,7} };
vector<interval> intervals(intvls, intvls+4);
for(int i=0;i<4;i++)
{cout << intervals[i].start;
} // This output is not coming when merge function is called
cout << merge(intervals, 4);
}
我的疑问是"当我评论合并函数调用时,即
// cout << merge(intervals, 4);
当我评论这一行时,我能够看到cout<<intervals[i].start.
的输出否则,我无法看到输出。
"
您不会用换行符结束输出。尝试:
{cout << intervals[i].start << "n";}
如果没有换行符,此输出可能会隐藏在 merge()
产生的所有输出中。
此代码几乎没有问题。
s.push(a[0]); <-- Only on element is in you stack.
//s.push(a[1]).. you will have to add all other elements like this.
while(i<n)
{
temp = s.top(); <-- for second i s will be empty. Here you must check if stack is empty before getting top element.
s.pop();
}
您的第一个输出未正确打印。 使用std::endl
cout << intervals[i].start << endl;
merge
中存在导致分段错误的错误。因此,std::cout
不会冲洗。如果使用合并调用注释掉该行,则程序退出时会刷新std::cout
。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 使用C++程序合并排序没有得到正确的输出
- 没有输出的合并排序我做错了什么?
- 合并排序没有给我任何输出
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 合并排序不正确的输出 c++
- 当许多线程的输出是可变长度时,我应该如何合并它?
- 当我使用此合并排序代码运行时,输出错误
- 输出结果时如何将参数化测试合并为一个
- C 合并的数组输出错误
- 基本合并排序中的输出不正确
- 调用合并函数时没有输出
- 随机输出与C++递归合并排序
- 合并排序输出问题
- 将两个文本文件合并为一个会给出不同的输出
- 如何合并2个std::映射,并在第三个映射中输出结果