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 use regex_iterator to extract and count words from string in C++

1 Answer

0 votes
#include <iostream> 
#include <iterator> 
#include <regex> 

int main() 
{ 
    const std::string s = "c++ python php java"; 
   
    std::regex words("[^\\s]+"); 
     
    auto words_begin = std::sregex_iterator(s.begin(), s.end(), words); 
    auto words_end = std::sregex_iterator(); 
 
    std::cout << "Total words = " << distance(words_begin, words_end); 
   
    std::cout << std::endl << std::endl << "Words list:" << std::endl; 
    for (std::sregex_iterator si = words_begin; si != words_end; si++) { 
        std::smatch match = *si; 
        std::string word = match.str(); 
        std::cout << word << std::endl; 
    } 
} 

 
/*
run:
 
Total words = 4

Words list:
c++
python
php
java
 
*/

 



answered Feb 8, 2020 by avibootz
edited Aug 21, 2024 by avibootz

Related questions

1 answer 98 views
1 answer 103 views
1 answer 117 views
2 answers 157 views
2 answers 174 views
...