简体   繁体   English

无法理解迭代器的问题?

[英]Can't understand the problem with iterator?

The problem states that we have to find the min no of students to remove so that the ith student can pass the exam. 问题表明,我们必须找到要删除的最小学生数,以便第i个学生可以通过考试。 So I am basically adding the students in a multiset as it stores sorted values and while the sorted sum is greater than required marks we subtract it out and move to the next one. 因此,我基本上是在多集中添加学生,因为它存储排序后的值,而排序后的总和大于所需的分数时,我们将其减去并移至下一个。

The problem comes with the input: 问题来自输入:

3 4 3 9 1 1 9 8 9

with m : required marks to pass being 14 与m:通过要求的分数为14

Here at the 6th index of input which is 9 which has not been added to the multiset is being deleted somehow. 在这里,以某种方式删除了输入的第6个索引,即9(尚未添加到多集)。

The output that i am getiing when running the troubled input: 运行有问题的输入时我得到的输出:

0 0 0 ;4--;3-- 2 ;9-- 1 ;9-- 1 ;9--;4--;9-- 3 ;9--;9--;9-- 3 ;9--;9--;9--;9-- 4

The values in :""-- contain the *x which being subtracted from sum there is an extra 9 but i don't know how? :“”-中的值包含* x,从总和中减去* x会得到额外的9,但是我不知道怎么办?

multiset<int> st;
    int setsum =0;
    for(int i=0;i<n;i++)
    {
        int sum = setsum+ar[i];
        if((sum)<=m)
        {
            cout<<"0 ";
        }
        else
        {
            //cout<<sum<<"-*";
            int cnt = 0;
            auto x = st.rbegin();
            while(sum>m)
            {
                sum -= *x;
                //cout<<";"<<*x<<"--";
                x--;
                //if(i==3)
                    //cout<<*x<<"++";
                cnt++;
            }
            cout<<" "<<cnt<<" ";
        }
        st.emplace(ar[i]);
        setsum += ar[i];
    }

Probably not the only problems, but I can't help but notice two major bugs in your use of reverse iterators: 可能不是唯一的问题,但是我不禁注意到在使用反向迭代器时的两个主要错误:

  1. You decrement the iterator ( --x ) instead of incrementing it ( ++x ); 您递减迭代器( --x )而不是递增( ++x ); the whole point of reverse iterators is that the direction is reversed, so you should be incrementing the iterator to move backwards through st . 反向迭代器的全部要点在于方向是反向的,因此您应该增加迭代器以通过st向后移动。 The only reason you'd use --x is if you had bidirectional iterator and wanted to move opposite the "natural" iteration order (so forward iterators would run backward, and reverse iterators would run forward). 使用--x的唯一原因是,如果您有双向迭代器,并且想以“自然”迭代顺序相反的方向移动(因此,正向迭代器将向后运行,而反向迭代器将向后运行)。
  2. You never check if you've reached the end of st ; 您永远不会检查是否已到达st的结尾; if you run off the end of st before sum > m (we have no definition of m , and thus no way to tell if this condition is necessarily true prior to running off the end of st ), you hit undefined behavior. 如果您在sum > m之前从st的末尾开始运行(我们没有m定义,因此在从st的末尾开始运行之前没有办法确定此条件是否一定成立),则会遇到未定义的行为。 The simplest fix is to simply update the test to while (sum > m && x != st.rend()) , though that may affect your code logic later on (since now exiting the loop isn't a guarantee that sum is less than or equal to m ), necessitating further tests. 最简单的解决方法是将测试更新为while (sum > m && x != st.rend()) ,尽管这可能会在以后影响您的代码逻辑(因为现在退出循环并不能保证sum较小大于或等于m ),则需要进一步测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM