how to initialize string pointer?

后端 未结 4 1523
再見小時候
再見小時候 2021-01-13 02:58

I want to store the static value in string pointer is it posible?

If I do like

string *array = {\"value\"};

the error occurs

4条回答
  •  余生分开走
    2021-01-13 03:23

    You can explicitly convert the literal to a string:

     std::string array[] = {std::string("value")};
    

    Note that you have to define this as an array, not a pointer though. Of course, an array mostly makes sense if you have more than one element, like:

    string array[] = {string("value1"), string("value2"), string("etc")};
    

提交回复
热议问题