MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / find_words

Function find_words

Examples/NoModules/Chapter 09/Ex9_02.cpp:28–42  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26}
27
28void find_words(vector<string>& words, string_view text, string_view separators)
29{
30 size_t start{ text.find_first_not_of(separators) }; // First word start index
31
32 while (start != string_view::npos) // Find the words
33 {
34 size_t end{ text.find_first_of(separators, start + 1) }; // Find end of word
35 if (end == string_view::npos) // Found a separator?
36 end = text.length(); // No, so set to end of text
37
38 words.push_back(std::string{ text.substr(start, end - start) }); // Store the word
39// Or: words.emplace_back(text.substr(start, end - start)); // (in-place construction)
40 start = text.find_first_not_of(separators, end + 1); // Find 1st character of next word
41 }
42}
43
44void list_words(const vector<string>& words)
45{

Callers 1

mainFunction · 0.70

Calls 1

push_backMethod · 0.45

Tested by

no test coverage detected