|
-
November 12th, 2005, 05:20 PM
#1
array manipulation
i have an array of strings
how do i take the last two elements, and insert them in positions [0] and [1] of the array respectively, and the original elements two places up?
thanks.
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
-
November 12th, 2005, 05:45 PM
#2
Re: array manipulation
If you are talking about C arrays, you need to move all the elements up two positions, one by one (starting on the top of course).
If you are not bound to use C arrays, use a container that allows inserting and removing on both sides, e.g. a deque or a list.
-
November 12th, 2005, 07:25 PM
#3
Re: array manipulation
I assume that you want to remove the last two elements
and move them to the beginning. That is (for an int example) :
0 1 2 3 4 5 6
change to
5 6 0 1 2 3 4
if so, you might be able to use std::rotate in <algorithm>, but it
depends on what your "array of strings" looks like.
-
November 12th, 2005, 08:06 PM
#4
-
November 12th, 2005, 08:24 PM
#5
Re: array manipulation
ok, currently i have
Code:
playerList.push_front(playerList.back());
playerList.pop_back();
playerList.push_front(playerList.back());
playerList.pop_back();
to achieve what i asked originally. Is there a more efficient way of using deque todo this?
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
-
November 13th, 2005, 05:12 AM
#6
Re: array manipulation
It may be possible to write something more efficient, but that one is already efficient.
Complexity is O(1).
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
November 13th, 2005, 06:56 AM
#7
Re: array manipulation
what would be the simplest way of seeing if a deque contains a particular element?
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
-
November 13th, 2005, 07:02 AM
#8
Re: array manipulation
there an easier way then this?
Code:
bool inList(deque<string>list, string str)
{
for(int i = 0; i < list.size(); i++)
if(list[i] == str)
return true;
return false;
}
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
-
November 13th, 2005, 10:33 AM
#9
Re: array manipulation
You can use the find algorithm:
Code:
return (find(list.begin(), list.end(), str) != list.end());
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
|