Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有 std::string 指针,我喜欢将它的值复制到普通的 std::string 我找不到快速快速的方法来做到这一点。我有这个 :
int main () { std::string * pStr = new std::string("hello") std::string strNew = pStr->??? // how to convert ? return 0; }
取消引用:
std::string strNew = *pStr;
两种方式:
std::string strNew = pStr->c_str(); // Be careful of `\0` with in the string
或者
第二个更好,因为 C 风格的字符串不能正确表示 std::string。它首先结束一个字符串\0并忽略尾随。
\0