#include <iostream>
#include <vector>
#include <string>
void function1(const char** keywords) { // C like
for (const char** keyword = keywords; *keyword; ++keyword) {
std::cout << *keyword << std::endl;
}
}
void function2(const std::vector<std::string>& keywords) { // C++11 like
for(const std::string& keyword : keywords) {
std::cout << keyword << std::endl;
}
}
int main() {
static const char* keywords[] = { "hello", "world", "!", 0 };
function1(keywords);
function2({ "hello", "world", "!" });
return 0;
}
Both functions do the same result. I like the new c++11 format of initialization where values are inline and I can skip empty value (zero). The issue is a performance. I expect that function2 will dynamically alocate memory on each invoke. I suppose it wan't be efficient. Is there a better way to implement such function with c++ manner keeping old C efficency?
Aucun commentaire:
Enregistrer un commentaire