Created
September 13, 2012 12:29
-
-
Save javagg/3714006 to your computer and use it in GitHub Desktop.
transform vector<string> to char* array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| } | |
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
Thanks