LoginSignup
3
3

More than 5 years have passed since last update.

スペース区切りで入力される値をvectorに読み込む

Posted at
helpers.hpp
#include<string>
#include<sstream>
#include<vector>
using namespace std;
vector<string> split(const string &s,char d){
    vector<string> r;
    size_t c=0,f;
    while((f=s.find_first_of(d, c))!=string::npos){
        r.push_back(string(s,c,f-c));
        c=f+1;
    }
    r.push_back(string(s,c,s.size()-c));
    return r;
}
template <typename T> vector<T> getValues(){
    string s;
    getline(cin,s);
    vector<string> v=split(s,' ');
    vector<T> r(v.size());
    for(int i=0 ; i<v.size() ; ++i){
        stringstream S;
        S<<v[i];
        S>>r[i];
        S.clear();
        S.str("");
    }
    return r;
}
template <typename T> void eraseValue(vector<T>&v,const T&d){
    v.erase(remove(v.begin(),v.end(),d),v.end());
}
3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3