fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. void print_combinations(const std::string& s)
  5. {
  6. if (s.size() >= 32) {
  7. return; // String too long
  8. }
  9. std::uint32_t end = 1u << s.size();
  10. for (std::uint32_t i = 0; i != end; ++i)
  11. {
  12. auto j = i;
  13. std::cout << "- ";
  14. for (const auto c : s)
  15. {
  16. if ((j & 1) != 0) {
  17. std::cout << c;
  18. }
  19. j >>= 1;
  20. }
  21. std::cout << std::endl;
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. print_combinations("abcd");
  28. }
  29.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
- 
- a
- b
- ab
- c
- ac
- bc
- abc
- d
- ad
- bd
- abd
- cd
- acd
- bcd
- abcd