Inserting a new element into a string array

I have the structure defined in the code below and need to insert a new string into the middle of the string array. I know there is a way to insert a new element with a vector. Does anyone know how to do this. I have tried several variations similar to uniqueList.word.insert(400,"disisdabomb"); but with no luck. Thank you in advance

const int maxWordCount=1500;
struct wordCountList
	string word[maxWordCount];
	int count[maxWordCount];
wordCountList uniqueList;
There's no "built in" way to do it. You'll have to do the insertion yourself. This means "moving" all the elements over 1 space to make room. You can do that fairly easily in a for loop.

EDIT: actually... rather than writing the for loop yourself... you could move them all with move_backward() (or copy_backward() if you're still not using C++11), defined in the <algorithm> header:


Once everything's moved over, you can just assign your new string into the array.

EDIT 2: in fact... the example on the move_backward page is doing pretty much exactly what you're trying to do.
Last edited on
If possible you can use std::string and all you have to do is <string_name>.insert(iterator/index,sub-string).
It will insert the string before the index there are more helpful overloads of this function as well as a insert_after() function.
Refer this site's references.
Topic archived. No new replies allowed.