【问题标题】:C++ string operation [duplicate]C ++字符串操作[重复]
【发布时间】:2020-09-13 18:26:25
【问题描述】:
string ss = "1-1-10-8-9";

上面有一个字符串。 我想在“-”之间的字符串数组中添加元素

字符串向量应与以下相同

vector<string> str = {"1", "1", "10", "8", "9"};

【问题讨论】:

标签: c++ string vector


【解决方案1】:
#include <iostream>
#include <sstream>
#include <vector>

int main() {

  std::stringstream ss("1-1-10-8-9");

  std::vector<std::string> v;
  std::string s;
  while(std::getline(ss, s, '-')) {
    v.push_back(s);
  }

  return 0;
}

【讨论】:

    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    相关资源
    最近更新 更多