分隔字符串数组 [英] Delimit string array

查看:66
本文介绍了分隔字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个存储在字符串数组中的文件中读取。我想使用stringstream将字符串分成几个部分,但这不会存储任何内容并为cout提供空输出:/

i have read from a file which are stored in a string array. i want to split string into several parts using stringstream, but this wont store anything and gives empty output for cout :/

        stringstream ssss;
ssss << arrayy[i];
getline(ss,line,' ');
first[i].country = line;
getline(ss,line,' ');
first[i].name = line;



但是上面没有给出任何输出:

和av使用空格作为分隔符


but above does not give any output:
and av to use space as delimiter

推荐答案

按照下面的例子:



follow the below example :

#include <iostream>
#include <sstream>
using namespace std;

int main () {

  int val;
  stringstream ss (stringstream::in | stringstream::out);

  ss << "120 42 377 6 5 2000";

  for (int n=0; n<6; n++)
  {
    ss >> val;
    cout << val*2 << endl;
  }

  return 0;
}</sstream></iostream>

输出:

output:

240
84
754
12
10
4000


尝试:

try:
        stringstream ssss;
ssss << arrayy[i];
ssss >>   first[i].country ;
ssss >>   first[i].name ;



但首先测试你的阵列[i]只包含2个单词,或者最后2个fisrt是正确的。 (我假设国家和地区名称是std :: string或simmilar类型的成员)。这将吃掉未使用的空格


but first test that your arrayy[i] contain just 2 words, or at last the 2 fisrt are the correct. (I´m assuming that country and name are members of type std::string or simmilar). This will eat not used spaces


这篇关于分隔字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆