CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2009
    Posts
    84

    [MFC] vector-shift-left

    Hello everyone!!!....

    I have a vector of n elements, and I'd want to shift it left, is there maybe a function that does it?...
    Like I have for example:

    1;2;3;4;5;6

    after shifting left I should have:

    2;3;4;5;6;7

    where I lost 1 and inserted 7 at last position...

    Thanks a lot
    Ciao
    Luigi

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: [MFC] vector-shift-left

    There is a function std::rotate() in <algorithm> (but you have to
    set the last element yourself). Here is an example using
    rotate with std::vector ... it also works with an array or CArray,
    \but the call will be slightly different:

    Code:
    std::vector<int> v;
    
    for (int i=0; i<6; ++i)
    {
       v.push_back(i+1);
    }
    
    std::rotate(v.begin(),v.begin()+1,v.end());
    v[5] = 7;

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: [MFC] vector-shift-left

    There is no vector class in MFC, and no, there is no function to do that automatically. You need to do that manually.

    If you use a list, you can remove the first element and and one at the tail.

    If you use an array, you have to copy the elements, one by one, from i+1 to i, and then set the value you want for the last element.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Aug 2009
    Posts
    84

    Re: [MFC] vector-shift-left

    HI cilu.... I'm implementing a MFC DLL and I have vectors... i'm using VisualStudio 2008... so thanks to Philip Nicoletti for the std::rotate() hint...

    btw, Philip, your last name sounds italian... :-)

    Ciao,
    Luigi

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: [MFC] vector-shift-left

    Quote Originally Posted by cilu View Post
    There is no vector class in MFC, and no, there is no function to do that automatically. You need to do that manually.

    If you use a list, you can remove the first element and and one at the tail.

    If you use an array, you have to copy the elements, one by one, from i+1 to i, and then set the value you want for the last element.

    There is a 'sort of' automatic way using MFC's CArray class. Store the first item, Delete the first item and add the former first to the end.
    Assuming int as type (should work for any type really).
    Code:
    int i = array[ array.GetUpperBound() ];
    array.RemoveAt(0);
    array.Add(i);

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: [MFC] vector-shift-left

    Quote Originally Posted by npuleio View Post
    HI cilu.... I'm implementing a MFC DLL and I have vectors... i'm using VisualStudio 2008... so thanks to Philip Nicoletti for the std::rotate() hint...
    When you prefixed your title with [MFC] I made the assumption you talk about MFC containers, that don't feature a "vector". How could we figure that you mean std::vector when you didn't mention that at all? You should ask the questions correctly, without ambiguities.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured