#include <fstream>
#include <string>
#include <cstring>
#include <map>
using namespace std;
struct Item {
int key;
char value[30];
Item() {};
Item(const int key, const string value) {
this->key = key;
if (value.length() > 30) {
throw "String is too long";
}
strcpy(this->value, value.c_str());
}
};
template<typename K, typename V>
map<V, K> invertMap(map<K, V> const& map) {
std::map<V, K> invMap;
for (auto const& pair : map) {
invMap.insert(std::make_pair(pair.second, pair.first));
}
return invMap;
}
template<typename K, typename V>
void printMap(map<K, V> const& map) {
for (auto const& item : map)
{
cout << item.first << " " << item.second << endl;
}
}
void writeTotextFile(const map<int, string> items, string filename) {
ofstream file("myfile1.txt");
for (auto const& item : items)
{
Item it(item.first, item.second);
file.write((char*)&it, sizeof(Item));
}
file.close();
}
void writeTotextFile(const map<string, int> items, string filename) {
ofstream file("myfile1.txt");
for (auto const& item : items)
{
Item it(item.second, item.first);
file.write((char*)&it, sizeof(Item));
}
file.close();
}
map<int, string> readFromtextFile(string filename) {
ifstream file("myfile1.txt");
map<int, string> items;
Item it;
while (file.read((char*)&it, sizeof(Item))) {
items.insert(std::make_pair(it.key, it.value));
}
file.close();
return items;
}
void printtxtFile(string filename) {
ifstream file("myfile1.txt");
Item it;
while (file.read((char*)&it, sizeof(Item))) {
cout << "Key: " << it.key << " Value: " << it.value << endl;
}
file.close();
}
int main() {
map<int, string> items = readFromtextFile("myfile1.txt");
writeTotextFile(items, "orderedByKey.txt");
writeTotextFile(invertMap(items), "orderedByValue.txt");
cout << "Sorted by key: " << endl;
printtxtFile("orderedByKey.txt");
cout << "--------------------------------" << endl;
cout << "Sorted by name: " << endl;
printtxtFile("orderedByValue.txt");
return 0;
}
What I have tried:
<pre>#include <fstream>
#include <string>
#include <cstring>
#include <map>
using namespace std;
struct Item {
int key;
char value[30];
Item() {};
Item(const int key, const string value) {
this->key = key;
if (value.length() > 30) {
throw "String is too long";
}
strcpy(this->value, value.c_str());
}
};
template<typename K, typename V>
map<V, K> invertMap(map<K, V> const& map) {
std::map<V, K> invMap;
for (auto const& pair : map) {
invMap.insert(std::make_pair(pair.second, pair.first));
}
return invMap;
}
template<typename K, typename V>
void printMap(map<K, V> const& map) {
for (auto const& item : map)
{
cout << item.first << " " << item.second << endl;
}
}
void writeTotextFile(const map<int, string> items, string filename) {
ofstream file("myfile1.txt");
for (auto const& item : items)
{
Item it(item.first, item.second);
file.write((char*)&it, sizeof(Item));
}
file.close();
}
void writeTotextFile(const map<string, int> items, string filename) {
ofstream file("myfile1.txt");
for (auto const& item : items)
{
Item it(item.second, item.first);
file.write((char*)&it, sizeof(Item));
}
file.close();
}
map<int, string> readFromtextFile(string filename) {
ifstream file("myfile1.txt");
map<int, string> items;
Item it;
while (file.read((char*)&it, sizeof(Item))) {
items.insert(std::make_pair(it.key, it.value));
}
file.close();
return items;
}
void printtxtFile(string filename) {
ifstream file("myfile1.txt");
Item it;
while (file.read((char*)&it, sizeof(Item))) {
cout << "Key: " << it.key << " Value: " << it.value << endl;
}
file.close();
}
int main() {
map<int, string> items = readFromtextFile("myfile1.txt");
writeTotextFile(items, "orderedByKey.txt");
writeTotextFile(invertMap(items), "orderedByValue.txt");
cout << "Sorted by key: " << endl;
printtxtFile("orderedByKey.txt");
cout << "--------------------------------" << endl;
cout << "Sorted by name: " << endl;
printtxtFile("orderedByValue.txt");
return 0;
}