[英]Writing c++ map of tuple and set to file
我有一个由元组作为键和集合作为值的无序映射。
我如何将此hash_DICT
写入文件?
#include <tuple>
#include <map>
#include <set>
int main() {
// Create dict here
map <tuple<int,int,int>,set<int>> hash_DICT;
for(int i =0; i < 10; i++){
tuple<int,int,int> tup_key = {i, i, i};
set<int> set_val = {i};
hash_DICT[tup_key] = set_val;
}
// Write hash_DICT to file here
return 0;
}
改编自https://www.stev.org/post/cppreadwritestdmaptoafile :
int WriteFile(std::string fname, std::map<std::tuple<int,int,int>,std::set<int>> *m) {
int count = 0;
if (m->empty())
return 0;
FILE *fp = fopen(fname.c_str(), "w");
if (!fp)
return -errno;
for(std::map<std::tuple<int,int,int>,std::set<int>>::iterator it = m->begin(); it != m->end(); it++) {
fprintf(fp, "%s=%s\n", it->first.c_str(), it->second.c_str());
count++;
}
fclose(fp);
return count;
}
但错误如下:
class "std::tuple<int, int, int>" has no member "c_str"
class "std::set<int, std::less<int>, std::allocator<int>>" has no member "c_str"
我应该如何修改它们才能写入文件并读取它们?
看看错误:
class "std::tuple<int, int, int>" has no member "c_str"
class "std::set<int, std::less<int>, std::allocator<int>>" has no member "c_str"
这些错误是不言自明的c_str()
是一个函数,它给出了const char *
(或 c 中的字符串),正如您所遵循的教程一样,它们使用了map<std::string, std::string>
但您map
由不同类型的元素组成。 这些错误现在有意义吗?
更新:当您要求从文件中读取和写入时,这里是完整的解决方案:
#include <tuple>
#include <map>
#include <set>
#include <iostream>
#include <fstream>
void read_from_file(const std::string &fileName, std::map<std::tuple<int, int, int>, std::set<int>> &targetMap) {
std::ifstream myStream(fileName);
if (!myStream.is_open()) {
std::cout << "Error opening file" << std::endl;
return;
}
int a, b, c, set_size;
while (myStream >> a >> b >> c >> set_size) {
std::tuple<int, int, int> tuple(a, b, c);
std::set<int> set;
for (int i = 0; i < set_size; i++) {
int val;
myStream >> val;
set.insert(val);
}
targetMap[tuple] = set;
}
}
void write_to_file(const std::string &fileName, const std::map<std::tuple<int, int, int>, std::set<int>> &sourceMap) {
std::ofstream myFile(fileName);
if (!myFile.is_open()) {
std::cout << "Failed to open file" << std::endl;
return;
}
for (const auto &it:sourceMap) {
myFile << std::get<0>(it.first) << " " << std::get<1>(it.first) << " " << std::get<2>(it.first) << " ";
myFile << it.second.size()
<< " "; // i'll be easier to set values read from file.. otherwise we need to use stringstream
for (const auto &it1: it.second) {
myFile << it1 << " ";
}
myFile << std::endl;
}
myFile.flush();
}
int main() {
// Create dict here
std::map<std::tuple<int, int, int>, std::set<int>> sourceMap;
for (int i = 0; i < 10; i++) {
std::tuple<int, int, int> tup_key = {i, i, i};
std::set<int> set_val = {i};
sourceMap[tup_key] = set_val;
}
write_to_file("file_1.txt", sourceMap);
// now let's read it..
sourceMap.clear();
read_from_file("file_1.txt", sourceMap);
for (const auto &it:sourceMap) {
std::cout << std::get<0>(it.first) << " " << std::get<1>(it.first) << " " << std::get<2>(it.first) << " ";
std::cout << it.second.size() << " ";
// it'll be easier to set values read from file.. otherwise we need to use stringstream
for (const auto &it1: it.second) {
std::cout << it1 << " ";
}
std::cout << std::endl;
}
return 0;
}
您可以非常轻松地使用ofstream
写入文件并访问正确的成员:
#include <tuple>
#include <map>
#include <set>
#include <iostream>
#include <fstream>
int main() {
// Create dict here
std::map<std::tuple<int, int, int>, std::set<int>> hash_DICT;
for (int i = 0; i < 10; i++) {
std::tuple<int, int, int> tup_key = {i, i, i};
std::set<int> set_val = {i};
hash_DICT[tup_key] = set_val;
}
std::ofstream myfile;
myfile.open("output.txt");
if (!myfile.is_open()) {
std::cout << "Failed to open file" << std::endl;
return 0;
}
for (const auto &it:hash_DICT) {
myfile << std::get<0>(it.first) << " " << std::get<1>(it.first) << " " << std::get<2>(it.first) << " --> ";
for (const auto &it1: it.second) {
myfile << it1 << " ";
}
myfile << std::endl;
}
myfile.flush();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.