C++ string split 字符串

2022/2/13 12:44:54

本文主要是介绍C++ string split 字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <cstdint>
#include <string.h>

void str_split(char* src,const char* split,std::vector<std::string> &res){
     char* ptr = nullptr;
     
     while((ptr = strsep(&src,split)) != nullptr){
         res.push_back(std::string(ptr));
     }
}

int main(void){
    std::string debug = "hello world test";
    std::vector<std::string> res;

    str_split(const_cast<char*>(debug.c_str()),std::string(" ").c_str(),res);

    for(int i = 0 ; i < res.size() ; i++){
        std::cout << res[i] << std::endl;
    }

    return 0;
}

主要就是const_cast<> 于普通指针之间的转换



这篇关于C++ string split 字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程