Here you can see how to use the initializer list:
initializer list
About your code here is a working code:
#include <iostream>
#include <vector>
#include <string>
void check(const std::vector<std::string>* vecptr) {
std::cout << "size of vecptr: " << vecptr->size() << std::endl;
}
int main() {
std::vector<std::string> vec{"hello", "there", "how", "are", "you"};
check(&vec);
return 0;
}
Output:
size of vecptr: 5
If you look on your declaration which is const std::vector<std::string>* vecptr
you can't pass directly check({"hello", "there", "how", "are", "you"});
because you need to pass the vector by reference. So if you want to use like in your example check({"hello", "there", "how", "are", "you"});
.
Change:
void check(const std::vector<std::string>* vecptr)
To:
void check(const std::vector<std::string>& vecptr)
Here is a full example:
#include <iostream>
#include <vector>
#include <string>
void check(const std::vector<std::string>& vecptr) {
std::cout << "size of vecptr: " << vecptr.size() << std::endl;
}
int main() {
check({"hello", "there", "how", "are", "you"});
return 0;
}
Output:
size of vecptr: 5