如何使用长字符串指定指针成员?

时间:2010-05-18 07:25:43

标签: c++ pointers

当我按照下面的做法擦除指针成员并为其指定新值时。

(*pMyPointer).member.erase();
(*pMyPointer).member.assign("Hello"); // Successfully

比我尝试过更多...

(*pMyPointer).member.erase();
(*pMyPointer).member.assign("Long Multi Lines Format String"); // How to?

如果长多行字符串不能用双引号引用,如何处理它。谢谢。

4 个答案:

答案 0 :(得分:3)

我真的不知道你想要问什么。也许这个:

(*pMyPointer).member.assign("Long Multi Lines Format String"
                            "more lines that will be"
                            "concatenated by the compiler");

或者你的意思是这样的换行:

(*pMyPointer).member.assign("Long Multi Lines Format String\n"
                            "more lines that will be\n"
                            "concatenated by the compiler");

答案 1 :(得分:2)

字符串文字中的换行符为'\n'

"This is a string literal\nwith a line break in it."

答案 2 :(得分:2)

我认为你的意思是传递一个非常长的字符串常量作为参数,在这种情况下C ++会为你进行字符串合并:printf("hello, " "world");printf("hello, world");

相同

因此:

(*pMyPointer).member.assign("Long Multi Lines Format String "
       "and here's more to the string "
       "and here's more to the string "
       "and here's more to the string "
       "and here's more to the string "
       "and here's more to the string ");

答案 3 :(得分:1)

我认为问题是如何创建一个多行字符串。

您可以轻松地执行以下操作:

(*pMyPointer).member.assign(
    "Long Multi Lines Format String" \
    "Long Multi Lines Format String" \
    "Long Multi Lines Format String"
 );

如果要返回,则必须在字符串中添加\ n。否则它会保持在同一条线上。