【问题标题】:How can I scan a string like this in c++?如何在 C++ 中扫描这样的字符串?
【发布时间】:2015-08-21 17:16:19
【问题描述】:

如果我有这样的输入,

apple+banana=3

我想将apple 存储在一个字符串中,banana 存储在另一个字符串中,3 存储在一个整数中,我该怎么做?我怎样才能跳过那些 += 标志?谢谢!

【问题讨论】:

标签: c++


【解决方案1】:

std::getline 将可选分隔符作为第三个参数,因此您可以执行以下操作:

#include <iostream>
#include <string>

int main() {
    std::string a, b;
    int c;
    std::getline(std::cin, a, '+');
    std::getline(std::cin, b, '=');
    std::cin >> c;
}

【讨论】:

  • 如果只是,apple+banana?那我该怎么做呢?谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 2021-06-24
  • 2015-06-20
相关资源
最近更新 更多