I have implemented the following program below for an assignment for practice on STL templates and algorithms. All I'm doing is implementing the code for the printing of empty files, un-empty files, etc.. I would love to know if there is any way to make the code more optimized.
Note: From the requirements of my school (Must be adhered to):
Code with the comment "helper function" should not be modified. These are
empty_check()
,split()
, andprint_filename()
.Functions commented as "Can modify" means the code within that function can be modified.
int main()
should not be modifiedNo extra headers allowed.
No definition of any new complex types or templates
No using of other functions other than the helper functions.
No use of lambda expressions.
No use of operators:
.
(member access)->
(member access via a pointer)*
(dereference).
No use of explicit iteration (
for
,while
,do while
) or selection (if
,switch
,?:
) statements or operators.No use of keyword
auto
.No use of
std::cout
,std::cerr
or any other functions that perform printing of the text. I'll have to use the provided helper function to do it.
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <functional>
#include <utility>
using name_file = std::string;
using sizeFile = size_t;
using record_in_file = std::pair<name_file, sizeFile>;
using file_datas = std::map<name_file, sizeFile>;
bool empty_check( //helper function
const record_in_file& dataRecord,
bool true_if_its_not
)
{
sizeFile size;
std::tie(std::ignore, size) = dataRecord;
bool result = (size == 0);
if (true_if_its_not)
{
result = !result;
}
return result;
}
name_file split(const record_in_file& dataRecord) //helper function
{
name_file name;
std::tie(name, std::ignore) = dataRecord;
return name;
}
void print_filename(const name_file& name1) //helper function
{
std::cout << " * " << name1 << std::endl;
}
void file_names_print(const file_datas& map1) //can modify
{
std::vector<name_file> files;
std::transform(std::begin(map1), std::end(map1), std::back_inserter(files), split);
std::for_each(std::begin(files), std::end(files), print_filename);
}
size_t files_un_empty_print(const file_datas& map1) //can modify
{
std::vector<record_in_file> files;
std::copy_if(std::begin(map1), std::end(map1), std::back_inserter(files),
std::bind(empty_check, std::placeholders::_1, true));
std::vector<name_file> file_names;
std::transform(std::begin(files), std::end(files), std::back_inserter(file_names),
split);
std::for_each(std::begin(file_names), std::end(file_names), print_filename);
return std::count_if(std::begin(map1), std::end(map1),
std::bind(empty_check, std::placeholders::_1, true));
}
size_t files_empty_print(const file_datas& map1) //can modify
{
std::vector<record_in_file> files;
std::copy_if(std::begin(map1), std::end(map1), std::back_inserter(files),
std::bind(empty_check, std::placeholders::_1, false));
std::vector<name_file> file_names;
std::transform(std::begin(files), std::end(files), std::back_inserter(file_names),
split);
std::for_each(std::begin(file_names), std::end(file_names), print_filename);
return std::count_if(std::begin(map1), std::end(map1),
std::bind(empty_check, std::placeholders::_1, false));
}
std::tuple<file_datas&> get_param(file_datas& map1) //can modify
{
return std::forward_as_tuple<file_datas&>(map1);
}
void empty_removal(file_datas& map1) //can modify
{
std::vector<record_in_file> files;
std::copy_if(std::begin(map1), std::end(map1), std::back_inserter(files),
std::bind(empty_check, std::placeholders::_1, true));
file_datas n_map{ std::begin(files),std::end(files) };
std::swap(map1, n_map);
}
int main()
{
file_datas map = {
{"readme.txt", 2000},
{"main.exe", 10000},
{"save.bak", 0},
{"library.dll", 1243},
{"0.res", 121100},
{"1.res", 121100},
{"2.res", 115600},
{"errors.log", 0}
};
std::cout << "Files:" << std::endl;
file_names_print(map);
std::cout << std::endl;
std::cout << "Files that are not empty:" << std::endl;
size_t Count_of_unemptyFiles = files_un_empty_print(map);
std::cout
<< " There are "
<< Count_of_unemptyFiles
<< " non-empty files.\n"
<< std::endl;
std::cout << "Files that are empty:" << std::endl;
size_t Count_of_emptyFiles = files_empty_print(map);
std::cout
<< " There are "
<< Count_of_emptyFiles
<< " empty files.\n"
<< std::endl;
std::cout << "Files after removing the empty ones:" << std::endl;
auto parameters = get_param(map);
std::apply(empty_removal, parameters);
file_names_print(map);
}
```