2010-05-18 27 views
0

當我做了下面的練習來擦除我的指針成員併爲其賦值時。如何分配長字符串的指針成員?

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

比我嘗試更多...

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

如果長時間多線串無法通過雙加引號引用,如何處理它。謝謝。在字符串

+1

這是什麼問題?問題在哪裏? – wilhelmtell 2010-05-18 07:32:13

+1

我是否有權假設您想知道如何編寫帶換行符的字符串文字?這與指針有什麼關係? – sbi 2010-05-18 07:32:55

+0

也許他有一種直覺,有一種更好的方法來訪問指針內的成員,並巧妙地詢問關於它的評論。 – wilhelmtell 2010-05-18 07:35:13

回答

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

+1:我猜你的猜測是一個很好的猜測。 – 2010-05-18 07:41:05

+0

@Binary:我想是的。 – 2010-05-18 12:25:05

2

換行符'\n'

"This is a string literal\nwith a line break in it." 
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 "); 
+0

啊!擊敗我! – Daniel 2010-05-18 13:52:30

1

我覺得這個問題是是如何創建一個多行字符串。

您可以輕鬆地做到這一點:

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

你必須到\ n添加到字符串,如果你想返回。否則,它將保持在同一條線上。