How to convert a vector<string> to a vector<char*>

convert image to vector illustrator
vector magic free
convert jpg to vector - illustrator
online vector creator
convert jpg to vector - photoshop
convert pdf to vector file free
how to make vector art
png to svg

This is the opposite of How to convert a vector<char*> to a vector<string>/string question.

I have some legacy routine which works with vector<char*> so I need to transform my vector<string>.

Here is what I come out with:

std::vector<char*> charVec(strVec.size(),nullptr);
for (int i=0; i<strVec.size();i++) {
    charVec[i]= new char(strVec[i].size()+1); 
    charVec[i][strVec[i].copy(charVec[i], strVec[i].size())] = '\0';
}

Is this correct?

Is there a better way to implement it?


p.s. of course at the end I have:

for (int i=0; i<strVec.size();i++) {
    delete charVec[i];
}

A faster way of doing this is

std::vector<const char*> charVec(strVec.size(),nullptr);
for (int i=0; i<strVec.size();i++) {
    charVec[i]= strVec[i].c_str();
}

and then using the resulting vector. This will save a lot of time on memory allocation for large sets of data.

Vector Magic: Convert JPG, PNG images to SVG, EPS, AI vectors, Online Vectorizer: Online raster to vector converter. Convert your images (jpeg, jpg or png) into scalable and clear vector art (svg,eps,dxf). TheAnswerHub is a top destination for finding answers online. Browse our content today! Find convert to vector file on TheAnswerHub.com.

Your new statement looks wrong. You want to allocate an array of char long enough to hold the string. You're allocating a single char whose value is the length of the string. I'm surprised you didn't get compiler warnings about that.

Try this:

std::vector<char*> charVec;
for (const auto &str : strVec) {
  char *charStr = new char[str.size() + 1];
  std::strcpy(charStr, str.c_str());
  charVec.push_back(charStr); 
}

The only drawback here is that, if one of the strings has embedded null characters, nothing beyond those will be copied. But I suspect that a function which takes a vector of char * almost certainly doesn't care about that anyway.

How to Convert Image into Vector Graphics with Adobe Illustrator , While most vector images are created from scratch, you can use image editing programs to "trace" JPG images and convert them to vectors. Search for Convert Jpg To Vector Format. Find It at ExamineJobs.com.

Why are you allocating new buffer for your strings? You are passing a vector of character pointers to a function and finally deleting allocated buffers. It means these buffers are temporary and there is no need to allocate memory for them as far as source vector(vector<string>) exist.

You can easily do this:

std::vector<std::string> vec1 = { "1", "2", "3" };
std::vector<const char*> vec2;

vec2.resize(vec1.size(), nullptr);

std::transform(std::begin(vec1), std::end(vec1), std::begin(vec2), [&](const std::string& str)
{
    return str.c_str();
});

Online image vectorizer, This makes vector images very useful for graphic designers. How to Convert Images to Vectors Due to these factors, often designers will choose to vectorize an  Easy to use Add the desired vector file from a device, Dropbox, Google Drive or by inserting a link, click the "Convert" button. Wait a little while the tool is working and save the result. Usually the process takes one or two minutes.

You could simply use string::c_str() to get the c string out of a std::string. Read more in How to convert a std::string to const char* or char*?

Or you could just modify a bit the body of the function, but only you know if this is worth it.


Of course as ssell mentions in the comments, keep in mind that with this approach you need not to destroy your strings when you handle them! It's a trade-off, you avoid the copy (so cool), but you need to be careful not reading junk (in case you delete your strings)!

Online Image Vectorizer, Converting vector files is now easy! Our web-based application helps you to convert vector files in seconds. Convertio — advanced online tool that solving any  Free vector converter. Convert raster to a vector image, eg, convert JPG to AI , PNG to AI (using the utility Potrace ). At the output you will get a black and white Convert the vector to vector format, eg, convert SVG to AI , AI to SVG , PDF to AI (using the UniConvertor ).

vector < string > to vector < const char* > : use c_str()

std::vector<const char*> charVec(strVec.size(), nullptr);
for (int i=0; i<strVec.size(); i++) {
    charVec[i] = strVec[i].c_str();
}

vector < string > to vector < char* > : use &x[0]

std::vector<char*> charVec(strVec.size(), nullptr);
for (int i=0; i<strVec.size(); i++) {
    charVec[i] = &strVec[i][0];
}

Refs: How to convert a std::string to const char* or char*?

How to Convert JPG to Vector (with Pictures), If you need to get a color vector image, you can use our autotrace tool with fine conversion settings. Convert the vector to vector format, eg, convert SVG to AI, AI to  Using GIMP and Inkscape 1. Download and install GIMP and Inkscape. These are free, open-source programs that you can use to create vector images 2. Open GIMP and use the Rectangle Select tool to select the part of the image that you want to convert to vector. 3. This will remove everything except

How can I "vectorize" an image?, Free online image to vector tool. PNG to SVG, JPG to SVG, and more. HOW TO CONVERT IMAGE TO VECTOR Step 1: Choose an image in PNG or JPG format​  To convert vector images, follow these steps: Use the "Local file" or "Online file" buttons to specify how to upload the image to the server. Use the "local file" if you need to convert a file from your computer, in order to specify a file on the Internet, select "Online file" and in the appeared field paste the link to the file.

Vector Converter (Online & Free), Wait a moment until the converting process is done and click the Download button to download the vector image. 2. Converting raster to vector using Vector Magic. The second tool you can use to convert raster to vector is Vector Magic. In addition to SVG, you can also choose ESP file format.

Free vector converter. Vectorize your image to AI, SVG, PDF and more., How To Convert JPG, PNG, GIF Images to PDF, SVG, EPS Vectors. 1. Upload. Upload a bitmap image and we automatically figure out what settings to use and trace the image for you. 2. Review & Edit. 3. Download.

Comments
  • What is your question?
  • do you have a question? looks like you already have it implemented
  • It depends if the new vector needs to own it's strings or it just needs a temporary view of the strings in the other vector.
  • Your result vector must be std::vector<const char*>. const char* is not assignable to char*
  • Thanks, I have corrected my answer. I always forget that c_str returns a const pointer.
  • Or you could replace vec2.resize() with vec2.reserve() and then use std::back_inserter(vec2) as the output iterator.
  • @RemyLebeau Of Course, good point. I had forgotten std::back_inserter. Thanks for remind.
  • I would not recommend std::string::c_str as it isn't safe. If any of his original std::string objects are destroyed, the char* string will become invalid. It is better to copy over the data as he does in his question and others do in their answers. See What is std::string::c_str() lifetime?.
  • @ssell good point, but I wouldn't go with the "do not touch" directive, I would like more the use with caution, I will update!
  • If he has strict control over the scope, then your answer is the most straight-forward. Just wanted to warn about the potential dangers.