how to copy a char pointer array into a vector in c++ ?-Collection of common programming errors

Hello Priya,

actually I would not recommend to use vector::push_back(). It will cause several reallocations which can be avoided if you know the size of the original structure. Instead I would recommend the constructor overload of vector which takes two iterators. If the vector already exists vector::insert will do fine also. Here is my suggestion:

//copying from a C char* array
char** var_name = new varName[100];

//copies elements 0 to 99
vector myVector ( &var_name[0], &var_name[100] );

//coping from a standard container
set haplotype;
//fill set with some data...

vector myVec;
myVec.reserve( haplotype.size() );
myVec.insert( haplotype.begin(), haplotype.end() );

best regards,
Markus Klein