-
January 27th, 2010, 10:05 AM
#1
[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
-
January 27th, 2010, 10:34 AM
#2
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;
-
January 27th, 2010, 10:37 AM
#3
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.
-
January 27th, 2010, 11:21 AM
#4
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
-
January 28th, 2010, 07:34 AM
#5
Re: [MFC] vector-shift-left
Originally Posted by cilu
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);
-
January 28th, 2010, 08:22 AM
#6
Re: [MFC] vector-shift-left
Originally Posted by npuleio
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|