The task is as follows: we enter text from the keyboard, the output should receive sentences line by line, and in order of increasing length of the sentence. Which structure is better to choose, so that later it would be easier to write a sorting algorithm? (I tried out the options vector<vector<char>> and vector<string> , but I’m dumb in writing the sorting algorithm by the length of the string.

  • What does the term "sentence" mean in your terms? And what sorting algorithm are you going to write? - Vlad from Moscow
  • A sentence is a set of words consisting of letters and a punctuation mark: full stop, question, exclamation. - Freddy
  • What about the sorting algorithm? - Vlad from Moscow
  • As I wrote below, the problem rests on how, when reading a string from several sentences, pushing them into a vector one by one - Freddy
  • This is another question. - Vlad from Moscow

3 answers 3

Execution on ideone

 #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<std::string> Vec = { "Мама", "Мама мыла раму", "Мама мыла" }; std::sort(Vec.begin(),Vec.end(),[](const std::string& a, const std::string& b) { return a.size()<b.size(); }); for(const auto &i:Vec) std::cout << i << std::endl; return 0; } 

Conclusion:

 Мама Мама мыла Мама мыла раму 
  • Thank! It helped) - Freddy
  • In principle, @VladD managed to write it even earlier. I just gave a compiled example. Grow Big) - Majestio

The easiest thing to take is vector<string> . To sort by length, use std::sort with a comparator.

 vector<string> v; // ... sort(v.begin(), v.end(), [](const string& l, const string& r) { return (l.length() < r.length()); }); 
  • So I did, the problem arose in that if you enter not one but several sentences into the string at a time, then how are they then one by one for pushing into the vector? - Freddy
  • @Freddy: Why enter a few in a string? Enter one by one. - VladD
  • @Freddy: If there are already a few of them in the line, well, then break it into pieces that correspond to the sentences. But this is another problem, not related to your question about sorting and data structure. - VladD
  • This is part of the task. At the input, I must enter as many sentences as I like. Then I throw them in this string, and string pushbatchu. I can not figure out whether to string the string into sentences and push-push parts, or somehow in the string in the process of reading them one by one. I do not know how to implement it. - Freddy
  • VladD, wrote everything correctly - Majestio

As I understand it, this is the task of creating your own abstract data structure and its sorting.

Otherwise, you could use the std::multiset container, in which the elements themselves would be ordered in ascending order lengths without any sorting. For example,

 #include <iostream> #include <string> #include <set> int main() { auto compare = [](const std::string &a, const std::string &b) { return a.length() < b.length(); }; std::multiset< std::string, decltype(compare)> sentences(compare); for (const std::string & s : { "123", "12", "12345", "1234", "1" }) { sentences.insert(s); } for (const auto &s : sentences) std::cout << s << std::endl; } 

Output of the program to the console

 1 12 123 1234 12345 

If you need to break the line into sentences, you can do it like this:

 #include <iostream> #include <string> #include <set> int main() { auto compare = [](const std::string &a, const std::string &b) { return a.length() < b.length(); }; std::multiset< std::string, decltype(compare)> sentences(compare); std::string text("I'm learning C++... How to write the program? I did it!"); for (std::string::size_type pos = 0; pos != text.size(); ) { auto last_pos = text.find_first_of(".?!", pos); if (last_pos == std::string::npos) { sentences.insert(text.substr(pos)); pos = text.size(); } else { while (last_pos < text.size() && text[last_pos] == '.') ++last_pos; sentences.insert(text.substr(pos, last_pos - pos + 1)); while (++last_pos < text.size() && (text[last_pos] == ' ' || text[last_pos] == '\t')); pos = last_pos; } } for (const auto &s : sentences) std::cout << s << std::endl; } 

The output of the program to the console:

 I did it! I'm learning C++... How to write the program? 

It is not efficient to use the std::vector container for such a task, since it will constantly redistribute memory when adding a new element, and then it will still need to be sorted.

If we talk about personally writing the data structure for storing sentences, then build a simply linked list. Also add new items to it in ascending order of keys.