0

I am trying to pass an initializer vector as a pointer to function check() as follows:

#include <bits/stdc++.h>
using namespace std;

void check(vector<std::string>* vecptr) {
    cout << "size of vecptr: " << vecptr->size() << endl;
}

int main() {
    check({"hello", "there", "how", "are", "you"});
    return 0;
}

But above code returns error: cannot convert ‘<brace-enclosed initializer list>’ to ‘std::vector<std::__cxx11::basic_string<char> >*.

How to pass such an hard coded initializer list as a pointer?

halfer
  • 19,824
  • 17
  • 99
  • 186
user3243499
  • 2,953
  • 6
  • 33
  • 75
  • 3
    what are you actually trying to achieve? Whats the actual problem you are trying to solve? Why is the parameter a pointer? – 463035818_is_not_an_ai Oct 20 '22 at 09:34
  • 1
    Why a pointer? The code would work fine if you just removed the `*` and replaced `->` by `.`. (Although you would typically use `std::initializer_list` instead of `std::vector` in such a situation.) – user17732522 Oct 20 '22 at 09:34
  • 2
    [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – 463035818_is_not_an_ai Oct 20 '22 at 09:34
  • 1
    Why would you pass a pointer to an initializer list to a function expecting a pointer to a vector? Why do you want to use pointers instead of references? _"What is the correct way to pass a initializer list of strings as a pointer to another function in C++?"_ There is no correct way to do this. – jabaa Oct 20 '22 at 09:38
  • 3
    Don't use pointers (when you don't have to), `void check(const std::vector& strings)`. Or for constant strings consider using string_view. – Pepijn Kramer Oct 20 '22 at 10:13

1 Answers1

0

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
gera verbun
  • 285
  • 3
  • 6