2014-10-21 20 views
0

乾杯!我對這個編碼社區完全陌生 - 但我到目前爲止非常享受它。如果我有一個字符串,並且cout的長度是多行,那麼我該如何分解這些行?分手長字串

例如:

string famousVillains; 
famousVillains = 
" 
Voldemort: skinny man with a fat backstory 
Ursula: fat lady with tentacles 
The Joker: scary dude with make-up 
Cruella: weird lady with dog obsession 
Terminator: crazy guy in black."; 

,當我清點這一點,我如何確保有每個惡棍之間的空間?我試着用< < endl;但這只是取消了後面的所有惡棍。

感謝您的幫助!

+0

請儘量使用正確的格式,顯示你試過的代碼,並給出確切* *所需的輸出,而不是handwaving。它會容易得多。 – luk32 2014-10-21 22:06:51

+0

當你說「空格」時,你的意思是換行嗎? – ooga 2014-10-21 22:07:54

+3

[C++多行字符串](http://stackoverflow.com/questions/1135841/c-multiline-string-literal)的可能的重複。但是你可能*真正想要的是構造你的數據;所以'class Character {private:string name;字符串描述; public:/ *方法在這裏* /};'然後用'vector famousVillains;'。這爲您提供了更多的選項來處理角色,對它們進行計數,添加和刪除它們以及顯示它們的各種方法。將所有數據存儲爲單個字符串對於處理來說更加艱鉅。 – HostileFork 2014-10-21 22:10:50

回答

1

您可以創建一個std::map或有小人及其說明的表查找。

struct Villain_Descriptions 
{ 
    std::string name; 
    std::string descripton; 
}; 

Villain_Descriptions famous_villains[] = 
{ 
    {"Voldemort", "skinny man with a fat backstory"}, 
    {"Ursula", "fat lady with tentacles"}, 
    {"The Joker", "scary dude with make-up"}, 
    {"Cruella", "weird lady with dog obsession"}, 
    {"Terminator", "crazy guy in black."}, 
}; 

查找表和std::map結構,可以通過搜索他們的名字得到一個小人的信息。
在你的方法中,你必須搜索一個字符串換行或名稱,然後提取子字符串。