serna37's Library

Logo

C++ アルゴリズムとデータ構造のライブラリ

View the Project on GitHub serna37/library-cpp

:heavy_check_mark: 文字列を区切る
(library/string/split.hpp)

文字列を区切る

できること

計算量

$O(\vert S \vert)$

使い方

vector<string> list = split(S, ",");

Verified with

Code

#pragma once
vector<string> split(const string &S, const char &sep) {
    vector<string> res = {""};
    for (auto &&v : S) {
        if (v == sep) {
            res.emplace_back("");
        } else {
            res.back() += v;
        }
    }
    return res;
}
#line 2 "library/string/split.hpp"
vector<string> split(const string &S, const char &sep) {
    vector<string> res = {""};
    for (auto &&v : S) {
        if (v == sep) {
            res.emplace_back("");
        } else {
            res.back() += v;
        }
    }
    return res;
}
Back to top page