Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用合并函数时没有输出_C++_Vector_Stack - Fatal编程技术网

C++ 调用合并函数时没有输出

C++ 调用合并函数时没有输出,c++,vector,stack,C++,Vector,Stack,我对以下代码有疑问。谁能解释一下吗 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<limit

我对以下代码有疑问。谁能解释一下吗

  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);
    }
使用名称空间std;
#定义整数大小32
#定义R4
#定义C4
#定义n4
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构间隔{
int启动;
内端;
};
布尔比较区间(区间i1、区间i2)
{
返回(i1.starttemp.end)
{
temp.end=a[i].end;
s、 推动(温度);
}
else if(temp.endcout您不能以换行结束输出。请尝试:

{cout << intervals[i].start << "\n";}

{cout此代码几乎没有问题

 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();
 }

merge
中存在导致分段错误的错误。因此,
std::cout
未刷新。如果使用merge调用注释掉该行,
std::cout
将在程序退出时刷新

 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();
 }
cout << intervals[i].start << endl;