How to insert and delete an element of a 1-D array.
for ex:
suppose the array is: 1 3 4 2 5
we want to insert 7 between 3 and 4 so that the new array is: 1 3 7 4 2 5

- 257,169
- 86
- 333
- 562

- 35
- 3
4 Answers
Use a std::vector
instead of a C-style array.

- 9,564
- 146
- 81
- 122

- 91,295
- 49
- 239
- 345
-
can you please give me the possible syntax with simple for loops and while loops – user451990 Sep 21 '10 at 17:43
-
4@user451990 - there is a wealth of STL vector iteration example code on this site. Could you track that down rather than requesting a repost here? – Steve Townsend Sep 21 '10 at 17:45
-
2And btw inserting like this gets expensive if your array is large. Consider std::list in that case (constant-time insert/delete) – Steve Townsend Sep 21 '10 at 17:46
-
1@Steve: That's a list of integers we're talking here. This would have to become _really_ large for a memory move on highly local data to be slower than a `std::list` with its abysmal locality. – sbi Sep 21 '10 at 18:30
-
@sbi - yes, for ints this is overkill. However, q refers to inserting into any 1D array, with int as an example. – Steve Townsend Sep 21 '10 at 18:59
-
@Steve: If in doubt, measure, change, measure again, compare (rinse, repeat). Until then, default to `std::vector` as a first choice. – sbi Sep 21 '10 at 19:39
-
@sbi - yes, your prodding drove me to this: http://stackoverflow.com/questions/2429217/under-what-circumstances-are-linked-lists-useful – Steve Townsend Sep 21 '10 at 19:54
-
1@Steve: Yes, in theory linked lists are faster than arrays when inserting/removing in the middle. In practice, however, `std::vector` often wins even in such cases (which is usually attributed to the higher locality of its data, which supposedly plays nicer with processor caches). Well, this applies to C++ at least. I don't know whether it also applies to languages that are interpreted and/or feature GC. – sbi Sep 21 '10 at 20:16
Arrays aren't very easy to deal with when you want to insert or remove an element. You would have to manually copy the data forward by 1 position to insert 1 element to make a new slot available.
To do this with less work to you, you can use an STL vector:
#include <vector>
//...
std::vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(4);
v.push_back(2);
v.push_back(5);
v.insert(v.begin() + 2, 7);
v.erase(v.begin());//Removing the first element just for fun
Even with a vector though you are still copying data (just transparently to you). So to do this most efficiently you would use an std::list
.
#include <list>
//...
std::list<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(4);
v.push_back(2);
v.push_back(5);
v.insert(++(++v.begin()), 7);
v.erase(v.begin());//Removing the first element just for fun

- 339,232
- 124
- 596
- 636
If you want to insert elements then using a list would be more appropriate -- arrays are sequential memory blocks so if you want to insert something you have to do work:
increase the size of the array
for each element past the spot - move one element further back
write new element at the target position

- 18,671
- 14
- 48
- 59
You can't do this with an array in C++ -- you'd have to reallocate the array and move the elements. Use a std::vector
instead, and the insert
method:
std::vector<int> v;
v.push_back( 1 );
v.push_back( 3 );
v.push_back( 4 );
v.push_back( 2 );
v.push_back( 5 );
// begin() yields an iterator to the first element; since it's a random-access
// iterator we can offset to insert at index 2.
v.insert( v.begin() + 2, 7 );
erase
can be used in a similar fashion to remove items.