zoukankan      html  css  js  c++  java
  • 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/
  • 相关阅读:
    UVA 1590 IP Networks
    UVA 12108
    HDUOJ 1042 N!
    UVA201 Squares
    UVaOJ 1339
    UVaOJ 202
    UVaOJ 100
    UVaOJ 10340
    第五章 跳跃表
    第四章 字典
  • 原文地址:https://www.cnblogs.com/bugcreator/p/11178610.html
Copyright © 2011-2022 走看看