Skip to content

Instantly share code, notes, and snippets.

@DragonOsman
Last active January 19, 2022 17:02
Show Gist options
  • Save DragonOsman/db74e4a38057b86ec6ae7861377ec5e9 to your computer and use it in GitHub Desktop.
Save DragonOsman/db74e4a38057b86ec6ae7861377ec5e9 to your computer and use it in GitHub Desktop.
// Osman Zakir
// 1 / 19 / 2022
// Beginning C++20: From Novice to Professional by Ivor Horton and Peter Van Weert
// Chapter 11 Exercise 4
// Exercise Specs:
/**
* Start again from the solution of Exercise 11-2 and move swap() and
* max_word_length() into an internals module implementation partition.
*/
import <iostream>;
import <string>;
import words;
int main()
{
using namespace std::string_literals;
words::Words words;
std::string text;
const auto separators{ " ,.!?\"\n"s };
std::cout << "Enter a string terminated by *:\n";
std::getline(std::cin, text, '*');
words::extract_words(words, text, separators);
if (words.empty())
{
std::cout << "No words in text." << std::endl;
return 0;
}
words::sort(words);
words::show_words(words);
}
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(5,18): error C7621: module partition 'internals' for module unit 'words' was not found
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,2): error C2672: 'swap': no matching overloaded function found
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,1): error C2780: 'void std::swap(std::array<_Ty,_Size> &,std::array<_Ty,_Size> &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\array(785): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,1): error C2780: 'void std::swap(std::unique_ptr<_Ty,_Dx> &,std::unique_ptr<_Ty,_Dx> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\memory(3447): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,1): error C2780: 'void std::swap(std::weak_ptr<_Ty> &,std::weak_ptr<_Ty> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\memory(3061): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,1): error C2780: 'void std::swap(std::shared_ptr<_Ty> &,std::shared_ptr<_Ty> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\memory(1918): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,1): error C2780: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(4679): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,1): error C2780: 'void std::swap(std::vector<_Ty,_Alloc> &,std::vector<_Ty,_Alloc> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vector(1983): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,1): error C2780: 'void std::swap(std::tuple<_Types...> &,std::tuple<_Types...> &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\tuple(795): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,1): error C2780: 'void std::swap(std::pair<_Ty1,_Ty2> &,std::pair<_Ty1,_Ty2> &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\utility(341): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,1): error C2780: 'void std::swap(_Ty (&)[_Size],_Ty (&)[_Size]) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(2022): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(27,1): error C2780: 'void std::swap(_Ty &,_Ty &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(2019): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,4): error C2672: 'swap': no matching overloaded function found
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,28): error C2780: 'void std::swap(std::array<_Ty,_Size> &,std::array<_Ty,_Size> &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\array(785): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,28): error C2780: 'void std::swap(std::unique_ptr<_Ty,_Dx> &,std::unique_ptr<_Ty,_Dx> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\memory(3447): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,28): error C2780: 'void std::swap(std::weak_ptr<_Ty> &,std::weak_ptr<_Ty> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\memory(3061): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,28): error C2780: 'void std::swap(std::shared_ptr<_Ty> &,std::shared_ptr<_Ty> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\memory(1918): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,28): error C2780: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(4679): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,28): error C2780: 'void std::swap(std::vector<_Ty,_Alloc> &,std::vector<_Ty,_Alloc> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vector(1983): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,28): error C2780: 'void std::swap(std::tuple<_Types...> &,std::tuple<_Types...> &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\tuple(795): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,28): error C2780: 'void std::swap(std::pair<_Ty1,_Ty2> &,std::pair<_Ty1,_Ty2> &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\utility(341): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,28): error C2780: 'void std::swap(_Ty (&)[_Size],_Ty (&)[_Size]) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(2022): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(36,28): error C2780: 'void std::swap(_Ty &,_Ty &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(2019): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,2): error C2672: 'swap': no matching overloaded function found
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,28): error C2780: 'void std::swap(std::array<_Ty,_Size> &,std::array<_Ty,_Size> &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\array(785): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,28): error C2780: 'void std::swap(std::unique_ptr<_Ty,_Dx> &,std::unique_ptr<_Ty,_Dx> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\memory(3447): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,28): error C2780: 'void std::swap(std::weak_ptr<_Ty> &,std::weak_ptr<_Ty> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\memory(3061): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,28): error C2780: 'void std::swap(std::shared_ptr<_Ty> &,std::shared_ptr<_Ty> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\memory(1918): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,28): error C2780: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(4679): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,28): error C2780: 'void std::swap(std::vector<_Ty,_Alloc> &,std::vector<_Ty,_Alloc> &) noexcept': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vector(1983): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,28): error C2780: 'void std::swap(std::tuple<_Types...> &,std::tuple<_Types...> &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\tuple(795): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,28): error C2780: 'void std::swap(std::pair<_Ty1,_Ty2> &,std::pair<_Ty1,_Ty2> &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\utility(341): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,28): error C2780: 'void std::swap(_Ty (&)[_Size],_Ty (&)[_Size]) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(2022): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(41,28): error C2780: 'void std::swap(_Ty &,_Ty &) noexcept(<expr>)': expects 2 arguments - 3 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(2019): message : see declaration of 'std::swap'
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter11ex4\chapter11ex4\words.cpp(75,33): error C3861: 'max_word_length': identifier not found
1>Done building project "chapter11ex4.vcxproj" -- FAILED.
module words;
std::size_t max_word_length(const words::Words& words)
{
std::size_t max{};
for (auto& pword : words)
{
if (max < pword->length())
{
max = pword->length();
}
}
return max;
}
void swap(words::Words& words, std::size_t first, std::size_t second)
{
auto temp{ words[first] };
words[first] = words[second];
words[second] = temp;
}
module words;
import <iostream>;
import <format>;
import :internals;
void sort(words::Words& words, std::size_t start, std::size_t end);
void words::sort(words::Words& words)
{
if (!words.empty())
{
::sort(words, 0, words.size() - 1);
}
}
void sort(words::Words& words, std::size_t start, std::size_t end)
{
// start index must be less than end index for 2 or more elements
if (!(start < end))
{
return;
}
// Choose middle address to partition set
// And swap middle address with start
swap(words, start, (start + end) / 2);
std::size_t current{ start };
for (std::size_t i{ start + 1 }; i <= end; i++)
{
// Is word word less than chosen word?
if (*words[i] < *words[start])
{
// Yes, so swap to the left
swap(words, ++current, i);
}
}
// Swap chosen and last swapped words
swap(words, start, current);
// Sort left subset if exists
if (current > start)
{
sort(words, start, current - 1);
}
// Sort right subset if exists
if (end > current + 1)
{
sort(words, current + 1, end);
}
}
void words::extract_words(words::Words& words, const std::string& text,
const std::string& separators)
{
std::size_t start{ text.find_first_not_of(separators) };
while (start != std::string::npos)
{
std::size_t end = text.find_first_of(separators, start + 1);
if (end == std::string::npos)
{
end = text.length();
}
words.push_back(std::make_shared<std::string>(text.substr(start, end - start)));
start = text.find_first_not_of(separators, end + 1);
}
}
void words::show_words(const words::Words& words)
{
const std::size_t field_width{ max_word_length(words) + 1 };
const std::size_t words_per_line{ 8 };
std::cout << std::format("{:{}}", *words[0], field_width);
std::size_t words_in_line{};
for (std::size_t i{ 1 }; i < words.size(); ++i)
{ // Output newline when initial letter changes or after 8 per line
if ((*words[i])[0] != (*words[i - 1])[0] || ++words_in_line == words_per_line)
{
words_in_line = 0;
std::cout << std::endl;
}
std::cout << std::format("{:{}}", *words[i], field_width);
}
std::cout << std::endl;
}
export module words;
import <memory>;
import <string>;
import <vector>;
export namespace words
{
using Words = std::vector<std::shared_ptr<std::string>>;
void sort(Words& words);
void extract_words(Words& words, const std::string& text, const std::string& separators);
void show_words(const Words& words);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment