P开发者_高级运维lease tell me how to delete an element from a C++ array.
My teacher is setting its value to 0, is that correct?
You can't really "delete" an element from a C++ array.
However, if the array consists of pointers, you can delete the object that a specific element points to.
In your teacher's case, it will be important to make note of whether the objects of the array are dynamically allocated (using the new
operator in C++) or not. He may simply be setting his values to 0
as an indicator that the value is no longer valid.
Without actual source code, this is as much as I can help you with.
If you're talking about a normal array, e.g.
int array[100];
then you can't "delete" an element, since the array always has 100 elements (in this example).
So it depends on the interpretation your program makes of the array values. If your teacher is consistently using a value of 0 to mean non-existent element, everything will work and so that's as correct as any other approach.
You can remove an element from a vector such that the element is no longer there and all the other elements shift a position.
struct counter
{
int x;
int operator()() { return x++; }
counter() : x(0) {}
};
std::vector<int> v;
std::generate_n( std::back_inserter(v), 8, counter() );
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(cout, " "));
std::cout << '\n';
v.erase( v.begin() + 4 );
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(cout, " "));
std::cout << '\n';
Should output:
0 1 2 3 4 5 6 7
0 1 2 3 5 6 7
(assume all necessary headers included and main function body etc).
Note that if you have a vector of pointers which were allocated with new, you would have to possibly call delete on the pointer before it was erased from the vector. (This depends on whether the vector manages the lifetime of these pointers). If you have a vector of boost::shared_ptr you will not need to manage the deletion.
If the array is not intended to be sorted, a quick and easy way is to copy the last element to the position of the element to be deleted, then reduce the element count by 1.
:
int main()
{
int a[100],x,i;
cout<<"enter the no. of elements(max 100): ";
cin>>x;
for(i=0;i<x;i++)
{
cout<<"enter "<<i+1<<" element: ";
cin>>a[i];
cout<<endl;
}
cout<<"your array: "<<endl;
for(i=0;i<x;i++)
{
cout<<a[i]<<endl;
}
cout<<"enter the element you want to delete: ";
int b,flag,pos;
cin>>b;
for(i=0;i<x;i++)
{
if(b==a[i])
{
flag=1; pos=i;
}
else
{
flag=0;
}
}
if(flag==0)
{
cout<<"element not found nothing to delete....";
}
for(i=0;i<x-1;i++)
{
if(i<pos)
{
a[i];
}
else if(i>=pos)
{
a[i]=a[i+1];
}
}
cout<<"new array:"<<endl;
for(i=0;i<x-1;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
Setting it to zero will still make that zero appear when you iterate over the array, or when you access it by index. If you do not want that, you have to copy all elements after the one you deleted one step towards the beginning of the array.
1) If you have an array of pointers, then like this :
// to create an array :
std::vector< int* > arr( 10, NULL );
for ( std::vector< int* >:iterator it=arr.begin; arr.end() != it; ++ it )
{
*it = new int( 20 );
}
// to delete one element
delete( arr.at(3) );
Any access to this array element is formally an undefined behaviour by the c++ standard. Even assignment of NULL, like this:
arr.at(3) = NULL;
2) If you really must use pointers, then use smart pointers (this specific case requires shared_ptr) :
std::vector< std::shared_ptr< int > > arr;
scanf("%ld",&x);//x position where we have to delete
if(x==n-1) {
n-=1;
}
else {
n-=1;
for (int i = x; i < n; i++) {
/* code */
A[i]=A[i+1];
}
}
it's an old topic but I'm gonna put this answer here for anyone who's recently seen this question!
deleting an element from the array is a heavy operation you can easily keep track of your elements with an index. anyway, you can call this function whenever you want to delete the first x
element of a vector
.
vector<int> remove_frist_x_items(const vector<int>& arr, int x)
{
return vector<int>(arr.begin() + x, arr.end());
}
// Delete the first element from the array
auto new_array = remove_frist_x_items(the_old_one, 1);
You can make another array by copying all the other element except the one you need to delete. And just delete the previous array using the below code line. (let's assume arr_name is the name of the array. if your array is like this,
int* arr_name = new int[5];
Then delete it using
delete[] arr_name;
cin>>n;
int array[n];
...
...
for(int k=0;k<n;k++){
array[k]=array[k+1]; //overwriting the current element of array with next element
array[n-1]=0; //setting last element as 0
--n; //reducing the size of array
}
...
...
精彩评论