Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to split string by delimiter into a vector in C++

1 Answer

0 votes
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>

template <typename Out>
void split(const std::string &s, char delimiter, Out v) {
    std::istringstream iss(s);
    std::string word;
    
    while (std::getline(iss, word, delimiter)) {
        if (word != "")
            *v++ = word;
    }
}

std::vector<std::string> split_by_delimiter(const std::string &s, char delimiter) {
    std::vector<std::string> v;
    
    split(s, delimiter , std::back_inserter(v));
    
    return v;
}

int main()
{
    std::string str = "C++:is:a::general:::purpose::::programming:::::language";
    
    std::vector<std::string> v = split_by_delimiter(str, ':');
    
    for(auto const& s : v)
        std::cout << s << "\n";
    
    return 0;
}




/*
run:
        
C++
is
a
general
purpose
programming
language
   
*/

 



answered Sep 3, 2021 by avibootz

Related questions

...