首页 > 编程语言 > 详细

C++ split分割字符串函数

时间:2018-01-07 18:18:32      阅读:216      评论:0      收藏:0      [点我收藏+]

将字符串绑定到输入流istringstream,然后使用getline的第三个参数,自定义使用什么符号进行分割就可以了。

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
void split(const string& s,vector<int>& sv,const char flag =  ) {
    sv.clear();
    istringstream iss(s);
    string temp;

    while (getline(iss, temp, flag)) {
        sv.push_back(stoi(temp));
    }
    return;
}

int main() {
    string s("123:456:7");
    vector<int> sv;
    split(s, sv, :);
    for (const auto& s : sv) {
        cout << s << endl;
    }
    system("pause");
    return 0;
}

 

C++ split分割字符串函数

原文:https://www.cnblogs.com/dingxiaoqiang/p/8228390.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!