• Advertisement

Archived

This topic is now archived and is closed to further replies.

Concatenating: characters->string->vector (string) :: C++

This topic is 5828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I would like to know how to concatenate characters into a string that is an element of a vector. For example: string dataTemp("today"); std::vector vecTemp; vecTemp.push_back(dataTemp); // I want to add characters into the tring element of vecTemp vecTemp[0] += " is Saturday." // Is that right? I would like vecTemp[0] to be "today is Saturday." Thanks, Kuphryn

Share this post


Link to post
Share on other sites
Advertisement
So add:

std::cout << vecTemp[0] << endl;

and you''ll see for yourself whether it''s right or not. Try first: ask second. By the way, no point calling it ''the string element'' of vecTemp as such, as all elements will be strings. Just clarifying your terminology.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
Okay.

I asked just to clarify that is the right way to get it working. Maybe there are better techniques.

Kuphryn

Share this post


Link to post
Share on other sites
There can''t really be a better technique. You''re asking to do something very trivial, ie. alter an element of an array. So, you look up the element (that''s the [0] bit) and you alter it (that''s the += bit). You can''t easily do much better than that.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
Okay.

There are always better algorithms. It is a matter of experience. Maybe using a specifi STL algorithm is faster.

Kuphryn

Share this post


Link to post
Share on other sites

  • Advertisement