fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template<class Iterator>
  5. void get_substrings_aux(std::vector<std::string>& subs, Iterator lo, Iterator hi) {
  6. if(lo == hi) return;
  7. const std::size_t n = subs.size();
  8. subs.reserve(2*n);
  9. auto c = *lo;
  10. for(std::size_t i = 0 ; i < n ; ++i) { subs.emplace_back(subs[i] + c); }
  11. get_substrings_aux(subs, ++lo, hi);
  12. }
  13.  
  14. std::vector<std::string> get_substrings(const std::string& str) {
  15. std::vector<std::string> subs(1);
  16. get_substrings_aux(subs, str.begin(), str.end());
  17. subs.erase(subs.begin());
  18. return subs;
  19. }
  20.  
  21. int main() {
  22. std::string str("1234");
  23. auto subs = get_substrings(str);
  24. for(const auto& s : subs) std::cout << s << std::endl;
  25. return 0;
  26. }
Success #stdin #stdout 0s 4488KB
stdin
Standard input is empty
stdout
1
2
12
3
13
23
123
4
14
24
124
34
134
234
1234