打破长串

时间:2014-10-21 22:02:46

标签: c++ string

干杯!我对这个编码社区来说是全新的 - 但到目前为止我非常享受它。如果我有一个字符串,并且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;但这只是取消了随后的所有恶棍。

感谢您的帮助!

1 个答案:

答案 0 :(得分: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结构允许您通过搜索他们的名字来获取恶棍的信息 在您的方法中,您必须在字符串中搜索换行符或名称,然后提取子字符串。

相关问题