Thread: How to restore consecutive enumeration

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    115

    How to restore consecutive enumeration

    Hello all

    I am a bit stuck with the following task. I have an array of groups of numbers (I call them classes), that might look like this:

    Code:
    static const int arr[] = {1,1,3,3};
    std::vector<int> classes( arr , arr + sizeof(arr) / sizeof( arr[ 0 ] ) );
    or like this

    Code:
    static const int arr[] = {1,3,3,3,5,5,6,8};
    std::vector<int> classes( arr , arr + sizeof(arr) / sizeof( arr[ 0 ] ) );
    The groups of numbers are in an increasing order, but not numbered consecutively. So, would I would like to have is this

    1,1,2,2

    and

    1,2,2,2,3,3,4,5

    A loop like this

    Code:
    for ( int i = 1; i < classes.size(); i++ )
        if ( classes.at( i ) > classes.at( i - 1 ) )
          classes.at( i ) = classes.at( i - 1 ) + 1;
    does not work, because it changes the preceding value in the group, thus destroying the group. How would you do it?

    Thank you for your help!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm not really sure how the ordering works yet, but since the grouping is significant one way is to add another dimension.
    Code:
    typedef vector<int> group;
    vector<group> classes;
    {
       int a[] = {1,1,2,2};
       int b[] = {1,2,2,2,3,3,4,5};
       classes.push_back(group(a, a + sizeof(a) / sizeof(a[0])));
       classes.push_back(group(b, b + sizeof(b) / sizeof(b[0])));
    }
    C++11 has something called initializer lists that makes this a lot less tedious. It would not surprise me if this worked:
    Code:
    typedef vector<int> group;
    vector<group> classes{ {1,1,2,2}, {1,2,2,2,3,3,4,5}};

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    115
    I think its more trivial than this. There is just one dimension (I provided two examples). I just need consecutive enumeration in the vector. I thought making a copy of the original vector (now temp) should solve the problem of overwriting the previous value:

    Code:
    static const int arr[] = {1,3,3,3,5,5,6,8};
    std::vector<int> temp( arr , arr + sizeof(arr) / sizeof( arr[ 0 ] ) );
    
    std::vector<int> classes( temp );
    for ( int i = 1; i < classes.size(); i++ )
        if ( temp.at( i ) > temp.at( i - 1 ) )
            classes.at( i ) = temp.at( i - 1 ) + 1;
    but somehow it does not.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You keep saying "consecutive enumeration", but I have no idea what you are really asking.

    Do you just want to modify the values so that the values stored become consecutive?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    And how is a list sorted in ascending order any less consecutive?

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by phantomotap View Post
    O_o

    You keep saying "consecutive enumeration", but I have no idea what you are really asking.

    Do you just want to modify the values so that the values stored become consecutive?

    Soma
    Yes!! So vector: 1,3,3,3,5,5,6,8

    has 5 subsequences of equal values {1}, {3,3,3}, {5,5}, {6}, {8}
    therefore the correct values should read: 1,2,2,2,3,3,4,5
    Last edited by serge; 05-10-2014 at 05:02 AM.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Yes!!
    O_o

    Look at your example code.

    You've done nothing to account for the indices of a given group; you are only looking at any two indices.

    Can you think of how you might account for the difference between the target value and current value?
    Can you think of how you might find the indices--the first and last index--of a given group?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by whiteflags View Post
    And how is a list sorted in ascending order any less consecutive?
    its increasing, but not consecutive, because the step size is not 1

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by phantomotap View Post
    O_o

    Look at your example code.

    You've done nothing to account for the indices of a given group; you are only looking at any two indices.

    Can you think of how you might account for the difference between the target value and current value?
    Can you think of how you might find the indices--the first and last index--of a given group?

    Soma
    ok, I got it. I missed and else statement:

    Code:
    static const int arr[] = {1,3,3,3,5,5,6,8};
    std::vector<int> temp( arr , arr + sizeof(arr) / sizeof( arr[ 0 ] ) );
     
    std::vector<int> classes( temp );
    for ( int i = 1; i < classes.size(); i++ )
        if ( temp.at( i ) > temp.at( i - 1 ) )
            classes.at( i ) = classes.at( i - 1 ) + 1;
        else
            classes.at( i ) = classes.at( i - 1 );

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Just out of curiosity, if the first element of your initial array is 42, what do you expect the first element to be after doing that?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to restore running app
    By WDT in forum C# Programming
    Replies: 4
    Last Post: 08-05-2009, 11:05 AM
  2. Win32 API Maiximize/Restore Down problem
    By Kennedy in forum Windows Programming
    Replies: 1
    Last Post: 08-11-2006, 10:26 PM
  3. log and restore
    By TopJo in forum C Programming
    Replies: 3
    Last Post: 07-01-2003, 07:14 PM
  4. Restore CD's
    By windoze victim in forum Tech Board
    Replies: 8
    Last Post: 03-10-2003, 05:50 AM
  5. windows restore
    By master2000 in forum Tech Board
    Replies: 8
    Last Post: 01-08-2003, 09:42 AM