You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cpp-exercises/ohai-const-strings.cpp

69 lines
1.6 KiB
C++

// how to convert std::vector<std::string> to const char**?
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
void print_strings(const std::vector<std::string> strings) {
using std::cout;
using std::endl;
cout << "== Printing some std::vector<std::string> strings:" << endl;
for (const std::string &s : strings) {
cout << s << endl;
}
}
const char** convert_to_c(const std::vector<std::string> strings) {
// Note: perfectly fine to "new" const char*.
const char** strings_c = new const char*[strings.size()+1];
for (size_t i = 0; i < strings.size(); ++i) {
// XXX No need to copy the string here.
char* string_c = new char[strings.at(i).size()+1];
strncpy(string_c, strings.at(i).c_str(), strings.at(i).size()+1);
strings_c[i] = string_c;
}
strings_c[strings.size()] = NULL;
return strings_c;
}
void free_c(const char** strings_c) {
for (size_t i = 0; strings_c[i] != NULL; ++i) {
delete[] strings_c[i];
}
delete[] strings_c;
}
void print_strings_c(const char** strings_c) {
using std::cout;
using std::endl;
cout << "== Printing some const char** strings:" << endl;
for (size_t i = 0; strings_c[i] != NULL; ++i) {
cout << strings_c[i] << endl;
}
}
void foo(const char* string) {
printf("foo: %s\n", string);
}
int main() {
foo("foo");
char* bar = new char[4];
strncpy(bar, "bar", 4);
foo(bar);
delete[] bar;
std::vector<std::string> some_strings = { "just", "some", "strings", "here" };
print_strings(some_strings);
const char **some_strings_c = convert_to_c(some_strings);
print_strings_c(some_strings_c);
free_c(some_strings_c);
}