No output

The way to swing the list follows the algorithm below :

1Pick the smallest integer from s and append it to the result.
2Pick the smallest integer from which is greater than the last appended integer to the result and append it.
3Repeat step 2 until you cannot pick more integers.
4Pick the largest integer from s and append it to the result.
5Pick the largest integer from s which is smaller than the last appended integer to the result and append it.
6Repeat step 5 until you cannot pick more integer.
7Repeat the steps from 1 to 6 until you pick all integers from s .


Input Format

The first line has a integer meaning there are n lists.

The next n line, each line has a list. The first integer l of a line is the length of the list. And followed integers are the elements of the list.

I've tried modifying the iterators, while the code below shows no output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <list>
#include <iostream>
#include <algorithm>
#include <set>
#include <iterator>
using namespace std;


int main() 
{
   int n,i;
    cin>>n;
    while(n--)
    {
       int L,val;
        cin>>L;  
        vector<int> A(L);
        vector<int>::iterator it,itr;
        A.clear();

       for(auto& a:A)
           cin>>a;
  
        sort(A.begin(),A.end());
 
       while(!A.empty())
       {
          val=*(A.begin());
          cout<<val<<" ";
          
          it=A.begin();
          for( i=0;i<A.size();i++)
          {
            
            if(*(it+i)>val)
            {
                val=*(it+i);
                cout<<val<<" ";
                itr=it+i;
                A.erase(itr);
                it--;
                
            }
          }   
           
          val=*(A.end()-1);
          cout<<val<<" ";
        
         it=(A.end()-1);
         for(i=0;i<A.size(); i++)
        {
          
            if(*(it-i)<val)
            {
                val=*(it-i);
                cout<<val<<" ";
                itr=it-i;
                A.erase(itr);
                it--;
                
            }
        }

       }
        cout<<"\n";
        
    }
    return 0;
}
Last edited on
L35 it is set to .begin()
L45 - --it it is now invalid as it points before .begin() !

Also don't forget that for std::vector .erase() invalidates any iterator pointing to or following the value(s) removed. If iterators are used, then .erase() returns a valid iterator. See http://www.cplusplus.com/reference/vector/vector/erase/

Again - use the debugger to trace through the code and identify issue(s).
Last edited on
But if I didn't do it-- in line 45 ,when I do A.erase(itr) in line 44 wouldn't the total number of iterators -1? that's why I thought there should be a it-- since it=A.begin() in line 35 was set before them.
Using the iterator on line 45 is illegal due to the erase on line 44. Why do you use the iterator at all? See:
1
2
3
4
5
6
7
8
9
10
11
12
13
          for( i=0;i<A.size();i++)
          {
            
            if(A[i]>val)
            {
                val=A[i];
                cout<<val<<" ";
                A.erase(A.begin() + i);
                --i; // Not skipping an element
            }
          }   
          if(not A.empty())
            val=A.back();
Topic archived. No new replies allowed.