码迷,mamicode.com
首页 > 编程语言 > 详细

C++ split分割字符串函数

时间:2018-01-07 20:02:49      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:ace   ++   etl   split   space   函数   turn   blog   system   

将字符串绑定到输入流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分割字符串函数

标签:ace   ++   etl   split   space   函数   turn   blog   system   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!