Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- //generic container split
- template<typename Cont>
- vector<Cont> split(Cont cont, typename Cont::value_type delim)
- {
- vector<Cont> r;
- Cont c;
- for(auto i : cont)
- {
- if(i != delim) c.push_back(i);
- else
- {
- r.push_back(c);
- c.clear();
- }
- }
- if(!c.empty()) r.push_back(c);
- return r;
- }
- int main() {
- string s = "this is a test";
- auto vr1 = split(s, ' ');
- for(auto i : vr1)
- cout << i << endl;
- cout << endl;
- vector<int> vi {1,2,3,4,5,9,3,2,1,9,8,7,6,5,9,4,5,6,7,8};
- auto vr2 = split(vi, 9);
- for(auto v : vr2)
- {
- for(const auto& i : v)
- cout << i;
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment