Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to copy std::vector<CString> to std::vector<CString>

Status
Not open for further replies.

Lorey

Programmer
Feb 16, 2003
88
SG
hi experts,

how to copy vector of Cstring to another vector of CString?

I got an strcore.cpp exception in my code when it goes to

UpdateData(const UpdateData& data) {
this->trainId = data.trainId;
this->track = data.track;
this->location = data.location;
this->rowText = data.rowText;
this->atsValid = data.atsValid;
this->communicationValid = data.communicationValid
}

specifically in rowText where its type std::vector<CString>

thanks!
 
If you're just trying to copy a single element into a vector you do something like:

Code:
this->rowText.push_back(data.rowText);

if you want to copy the entire vector you need an iterator.


so you would run through the entire vector

Code:
vector<CString>::iterator it = data.rowText.Begin();

for(; it != data.rowText.End(); it++)
{
   this->rowText.push_back(*it);
}
 
or even easier to copy the whole vector:
Code:
this->rowText.assign( data.rowText.begin(), data.rowText.end() );
 
Thanks for the replies.

this UpdateData function gets called every like 50 milliseconds. And the rowText's size would only be 3. Which is the most safest and fastest way to go, iteration, or .assign?

do i need to new each CString data? My debug assertion is prompted to strcore.cpp 154.

regards,
lorna
 
The vector::assign() function should basically do what SDowd suggested, but it may be able to take advantage of some additional optimizations because it has access to the private internal data of the vector. So assign() is faster or equal to looping manually, but it should never be slower.
 
I would go with whatever cpjust says :) I'm sure he knows much more than I :)
 
What's wrong with just assigning it like you did originally?
Code:
this->rowText = data.rowText;
 
Oops... forgot about the error. The assignment you were using is correct, the error is caused by something else. Switching to assign or a loop will at best leave it there and at worst mask the real problem.

Are you sure the objects are valid when you call UpdateData?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top