fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5.  
  6. int main()
  7. {
  8. char wordList[] = "_cT68est aaa xyz abc_79d A_bcd usw8 bWort";
  9.  
  10. struct wordInformation
  11. {
  12. char const* pos;
  13. std::size_t len;
  14. };
  15.  
  16. std::vector<wordInformation> vec;
  17. vec.reserve( sizeof(wordList) / 5 ); // Durchschnittliche Wortlänge +1
  18.  
  19. bool last_char = false;
  20. for( auto p = wordList; p != wordList + sizeof(wordList) - 1; ++p )
  21. {
  22. if( !std::isspace(*p) )
  23. {
  24. if(!last_char)
  25. {
  26. vec.push_back({p, 1});
  27. last_char = true;
  28. }
  29. else
  30. ++vec.back().len;
  31. }
  32. else if(last_char)
  33. last_char = false;
  34. }
  35.  
  36. std::sort( std::begin(vec), std::end(vec), [](wordInformation const& p1,
  37. wordInformation const& p2)
  38. { return std::lexicographical_compare(p1.pos, p1.pos + p1.len,
  39. p2.pos, p2.pos + p2.len); } );
  40.  
  41. char sortedWordList[sizeof(wordList)];
  42.  
  43. auto ptr = sortedWordList;
  44. for( auto& info : vec )
  45. {
  46. while( info.len-- )
  47. *ptr++ = *info.pos++;
  48.  
  49. *ptr++ = ' ';
  50. }
  51.  
  52. sortedWordList[sizeof(sortedWordList)-1] = '\0';
  53. std::cout << sortedWordList;
  54. }
  55.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
A_bcd _cT68est aaa abc_79d bWort usw8 xyz