拆分字符串C ++ [英] Splitting String C++

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

问题描述

在C ++中,如果我有一个字符串,我该如何将它拆分成令牌?

Hi in C++ if i have a string, how can I split this into tokens?

推荐答案

:),它将结果放在 elems 中。 delim 可以是任何 char

this works nicely for me :), it puts the results in elems. delim can be any char.

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

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

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