I want a function that takes as input a vector of strings that I don't want to modify. So I thought about taking as input a vector<string_view>
. However, if I do this, I can't pass a vector<string>
as a parameter anymore.
Example:
#include <vector>
#include <string>
#include <string_view>
using namespace std;
void takesStringViewVectorRef(vector<string_view> const & v) {}
int main ()
{
string_view strview; string str;
takesStringViewVectorRef({"a", "b", strview, str}); // works, cool!
vector<string> vec;
takesStringViewVectorRef(vec); // doesn't work
vector<const char *> vec2;
takesStringViewVectorRef(vec2); // doesn't work either
return 0;
}
Is this just a bad idea? Should I stick with vector<string>
? I'm a little confused about this
I would create a template function that can work on the string type you put in like this: