Skip to content

Instantly share code, notes, and snippets.

@javagg
Created September 13, 2012 12:29
Show Gist options
  • Select an option

  • Save javagg/3714006 to your computer and use it in GitHub Desktop.

Select an option

Save javagg/3714006 to your computer and use it in GitHub Desktop.
transform vector<string> to char* array
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> symbols = {"IF1209", "IF1210"};
std::vector<const char *> chars(symbols.size());
std::for_each(symbols.begin(), symbols.end(), [](std::string& s) {
std::cout << s << std::endl;
});
std::transform(symbols.begin(), symbols.end(), chars.begin(), std::mem_fun_ref(&std::string::c_str));
std::for_each(chars.begin(), chars.end(), [](const char *s) {
std::cout << s << std::endl;
});
return 0;
}
@smac89
Copy link

smac89 commented Mar 6, 2025

Just a warning

This is not safe. Once symbols goes out of scope, the contents of chars become invalid and will lead to all sorts of memory related issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment