Guys,
i have a problem in insert a string in to a vector.To be clear enough i have insert "vishwanath" into a vector


can any1 help...

anytime you have to insert something somewhere in a vector, other than at the very end, then ye' should not use a vector. consider using queue or deque.

vector is a template array, you can insert a element into any where you want;
like this

vector<string>vData;
vData[5] = "adfa";

vector is a template array, you can insert a element into any where you want;
like this

vector<string>vData;
vData[5] = "adfa";

Techincally, that's "replacing an element." Inserting implies moving other values out of the way. Specifically in an array or std::vector<> situation, that involves moving each element, from the desired insertion-point to the end of the array, one position farther along the array -- usually starting with the last element in the array and working backwards so you don't inadvertently overwrite any of the others. This is why the previous responder recommended using a different container class. Even a std::list<> would be an improvement for arbitrary insertion.

Of course, if the OP just wants to stick strings in a container, and relative position within the container isn't important, then a vector is a reasonable choice, and the append() method should work fine. The choice of a container depends perhaps most on what you want to do with the data -after- you input it.

This article has been dead for over six months. Start a new discussion instead.