I have an array int *playerNum
which sto开发者_JAVA技巧res the list of all the numbers of the players in the team. Each slot e.g playerNum[1];
represents a position on the team, if I wanted to add a new player for a new position on the team. That is, inserting a new element into the array somewhere near the middle, how would I go about doing this?
At the moment, I was thinking you memcpy
up to the position you want to insert the player into a new array and then insert the new player and copy over the rest of it?
(I have to use an array)
If you're using C++, I would suggest not using memcpy
or memmove
but instead using the copy
or copy_backward
algorithms. These will work on any data type, not just plain old integers, and most implementations are optimized enough that they will compile down to memmove
anyway. More importantly, they will work even if you change the underlying type of the elements in the array to something that needs a custom copy constructor or assignment operator.
If you have to use an array, after having made sure you have enough storage (using realloc
if necessary), use memmove
to shift the items from the insertion point to the end by one position, then save your new player at the desired location.
You can't use memcpy
if the source and target areas overlap.
This will fail as soon as the objects in your array have non-trivial copy-constructors, and it's not idiomatic C++. Using one of the container classes is much safer (std::vector
or std::list
for instance).
Your solution using memcpy
is correct (under few assumptions mentionned by other).
However, and since you are programming in C++. It is probably a better choice to use std::vector
and its insert
method.
vector<int> myvector (3,100);
myvector.insert ( 10 , 42 );
An array takes a contiguous block of memory, there is no function for you to insert an element in the middle. you can create a new one of size larger than the origin's by one then copy the original array into the new one plus the new member
for(int i=0;i<arSize/2;i++)
{
newarray[i]<-ar[i];
}
newarray[i+1]<-newelemant;
for(int j=i+1<newSize;j++,i++)
{
newarray[i]<-ar[i];
}
if you use STL, ting becomes easier, use list.
As you're talking about an array and "insert" I assume that it is a sorted array. You don't necessarily need a second array provided that the capacity N
of your existing array is large enough to store more entries (N>n
, where n
is the number of current entries). You can move the entries from k
to n-1
(zero-indexed) to k+1
to n
, where k
is the desired insert position. Insert the new element at index position k
and increase n
by one. If the array is not large enough in the beginning, you can follow your proposed approach or just reallocate a new array of larger capacity N'
and copy the existing data before applying the actual insert operation described above.
BTW: As you're using C++, you could easily use std::vector
.
While it is possible to use arrays for this, C++ has a better solutions to offer. For starters, try std::vector
, which is a decent enough general-purpose container, based on a dynamically-allocated array. It behaves exactly like an array in many cases.
Looking at your problem, however, there are two downsides to arrays or vectors:
- Indices have to be 0-based and contiguous; you cannot remove elements from the middle without losing key/value associations for everything after the removed element; so if you remove the player on position 4, then the player from position 9 will move to position 8
- Random insertion and deletion (that is, anywhere except the end) is expensive - O(n), that is, execution time grows linearly with array size. This is because every time you insert or delete, a part of the array needs to be moved.
If the key/value thing isn't important to you, and insertion/deletion isn't time critical, and your container is never going to be really large, then by all means, use a vector. If you need random insertion/deletion performance, but the key/value thing isn't important, look at std::list
(although you won't get random access then, that is, the []
operator isn't defined, as implementing it would be very inefficient for linked lists; linked lists are also very memory hungry, with an overhead of two pointers per element). If you want to maintain key/value associations, std::map
is your friend.
Losting the tail:
#include <stdio.h>
#define s 10
int L[s];
void insert(int v, int p, int *a)
{
memmove(a+p+1,a+p,(s-p+1)*4);
*(a+p) = v;
}
int main()
{
for(int i=0;i<s;i++) L[i] = i;
insert(11,6, L);
for(int i=0;i<s;i++) printf("%d %d\n", L[i], &L[i]);
return 0;
}
精彩评论