• c++ split(getline实现)


    众所周知

    c++中string没有自带的split函数(亏你还是老大哥)

    网上关于split函数的优秀写法很多

    本人不再赘述

    今几日翻C++API时发现了getline一个有趣的方法

    istream& getline (istream& is, string& str, char delim);

    第一个参数是一个输入流,第二个参数是一个对字符串的常引用,第三个参数是分割符。

    在读入时遇到分割符则停止

    可以用这个来实现单分割符的split功能

     1 #include <iostream>
     2 #include <string>
     3 #include <sstream>
     4 using namespace std;
     5 
     6 int main() {
     8     stringstream input("45,65,45231,4646,4564");
     9     string str;
    11     while (getline(input, str, ',')) {
    12         cout << str << endl;
    13     }
    15     return 0;
    16 }

    简单方便快速。 

    博客迁移到https://luotianqi777.github.io/
  • 相关阅读:
    C
    B
    D
    I
    B
    E
    B
    L
    H
    bzoj3276磁力 两种要求下的最大值:分块or线段树+拓扑
  • 原文地址:https://www.cnblogs.com/bugcreator/p/11178610.html
Copyright © 2020-2023  润新知