【问题标题】:Finding a match to a string with getline使用 getline 查找字符串的匹配项
【发布时间】:2013-03-21 11:23:34
【问题描述】:

我正在尝试在字符串名称向量中搜索匹配项。名称可以是多个单词,因此在提示用户时我正在使用 getline(std::cin, name);但是我永远找不到与向量中现有名称的匹配项。如何从 getline 返回中删除额外的“垃圾”,使其与向量中的内容相匹配?

【问题讨论】:

  • 你见过什么样的extra garbage

标签: c++ match getline


【解决方案1】:

使用分隔符“\0”。

即:

getline(cin, name, '\0');

并检查一下: http://www.cplusplus.com/reference/string/string/getline/

还有这个: http://www.cplusplus.com/reference/istream/istream/ignore/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多