Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c+中高效地创建和编译大型地图+; 目前,我有多个JSON文件和一个Python代码,其中读取JSON文件MIN .JSON中的一个,并根据JSON文件创建完整的C++代码。在C++的主体()中,我需要使用NUBBETSTRIG.JSON将数字输出转换为字符串。我无法找到一种及时编译C++代码的方法(),你绝对不能自动生成C++代码,这样C++会硬编码这样的地图。代码的编译速度要快得多,读取20000个文件所需的时间应该在几毫秒左右(而不是编译所需的>5分钟) 如果您想避免在C++代码中添加JSON解析器的依赖项,建议将JSON文件转换成更简单的格式,用C++更容易阅读。 从文件中读取和写入地图的一种非常简单的格式_Python_C++_Json_Enums_Maps - Fatal编程技术网

如何在c+中高效地创建和编译大型地图+; 目前,我有多个JSON文件和一个Python代码,其中读取JSON文件MIN .JSON中的一个,并根据JSON文件创建完整的C++代码。在C++的主体()中,我需要使用NUBBETSTRIG.JSON将数字输出转换为字符串。我无法找到一种及时编译C++代码的方法(),你绝对不能自动生成C++代码,这样C++会硬编码这样的地图。代码的编译速度要快得多,读取20000个文件所需的时间应该在几毫秒左右(而不是编译所需的>5分钟) 如果您想避免在C++代码中添加JSON解析器的依赖项,建议将JSON文件转换成更简单的格式,用C++更容易阅读。 从文件中读取和写入地图的一种非常简单的格式

如何在c+中高效地创建和编译大型地图+; 目前,我有多个JSON文件和一个Python代码,其中读取JSON文件MIN .JSON中的一个,并根据JSON文件创建完整的C++代码。在C++的主体()中,我需要使用NUBBETSTRIG.JSON将数字输出转换为字符串。我无法找到一种及时编译C++代码的方法(),你绝对不能自动生成C++代码,这样C++会硬编码这样的地图。代码的编译速度要快得多,读取20000个文件所需的时间应该在几毫秒左右(而不是编译所需的>5分钟) 如果您想避免在C++代码中添加JSON解析器的依赖项,建议将JSON文件转换成更简单的格式,用C++更容易阅读。 从文件中读取和写入地图的一种非常简单的格式,python,c++,json,enums,maps,Python,C++,Json,Enums,Maps,让我们看一张简单的地图: map<string, map<string, string>> test_map { {"Hello", { {"A", "B"}, {"C", "D"}}}, {"World", { {"Blarg", "glug glug glug"}, {"idek what to put here", "felt cute might delete later"}}}}

让我们看一张简单的地图:

map<string, map<string, string>> test_map {
    {"Hello", {
        {"A", "B"}, 
        {"C", "D"}}},
    {"World", {
        {"Blarg", "glug glug glug"}, 
        {"idek what to put here", "felt cute might delete later"}}}}; 
您有2,这意味着顶级映射有2个元素。后面是5,这意味着第一个键中有5个字符。后面是第一个映射的键和值,等等

将映射写入此格式的文件 因为格式很简单,所以做起来也很简单

namespace output {
    using std::map; 
    using std::string; 
    void write(FILE* file, string const& str) {
        // Write the length of the string, followed by a space
        fprintf(file, "%lu ", str.size());

        // Write the string itself
        fwrite(str.data(), 1, str.size(), file);  
    }

    template<class Key, class Value>
    void write(FILE* file, map<Key, Value> const& m) {
        // Write the length of the map, followed by a space
        fprintf(file, "%lu ", m.size()); 

        for(auto& entry : m) {
            // Write the key
            write(file, entry.first);

            // Write the value
            write(file, entry.second); 
        }
    }
}
名称空间输出{
使用std::map;
使用std::string;
无效写入(文件*文件、字符串常量和字符串){
//写入字符串的长度,后跟空格
fprintf(文件“%lu”,str.size());
//写字符串本身
fwrite(str.data(),1,str.size(),文件);
}
样板
无效写入(文件*文件,映射常量(&m){
//写下地图的长度,后跟空格
fprintf(文件“%lu”,m.size());
用于(自动输入:m){
//写钥匙
写入(文件,条目。第一);
//写下值
写入(文件、条目、秒);
}
}
}
从文件中读取地图 这也很简单,例如,要读取字符串,我们先读取长度,然后读取所有字符

namespace input {
    using std::map;
    using std::string; 

    void read(FILE* file, size_t& length) {
        int result = fscanf(file, "%lu ", &length);
        if(result < 0) throw std::logic_error("Couldn't read from file"); 
    }

    void read(FILE* file, string& str) {

        size_t length;      // Read the length
        read(file, length); 

        str.resize(length); 
        size_t n_read = fread(&str[0], 1, length, file); // Read the characters

        if(n_read != length) { // Handle errors
            throw std::logic_error("Unable to read entirety of string from file"); 
        }
    }

    template<class Key, class Value>
    void read(FILE* file, map<Key, Value>& text) {
        size_t length;      // Read the length of the map
        read(file, length); 
        text.clear(); 

        for(size_t i = 0; i < length; i++) {
            Key key;
            read(file, key);        // Read the key
            read(file, text[key]);  // Read the value
        }
    }
}
名称空间输入{
使用std::map;
使用std::string;
无效读取(文件*文件、大小和长度){
int result=fscanf(文件“%lu”,&length);
如果(结果<0)抛出std::logic_错误(“无法从文件中读取”);
}
无效读取(文件*文件、字符串和字符串){
size\u t length;//读取长度
读取(文件、长度);
str.resize(长度);
size_t n_read=fread(&str[0],1,长度,文件);//读取字符
如果(n_read!=length){//处理错误
抛出std::logic_错误(“无法从文件中读取整个字符串”);
}
}
样板
无效读取(文件*文件、地图和文本){
size\u t length;//读取地图的长度
读取(文件、长度);
text.clear();
对于(大小i=0;i
使用此代码 要编写地图:

void write_map(string file, map<string, map<string, string>> test_map) {
    auto output_file = fopen(file.c_str(), "w"); 
    output::write(output_file, test_map); 
    fclose(output_file); 
}
void写入映射(字符串文件、映射测试映射){
自动输出文件=fopen(file.c_str(),“w”);
输出::写入(输出文件、测试映射);
fclose(输出文件);
}
要阅读地图:

map<string, map<string, string>> read_map(string file) {
    auto input_file = fopen(file.c_str(), "r"); 
    map<string, map<string, string>> m;
    input::read(file, m); 
    fclose(input_file); 
    return m; 
}
map read\u映射(字符串文件){
自动输入文件=fopen(file.c_str(),“r”);
地图m;
输入::读取(文件,m);
fclose(输入_文件);
返回m;
}
测试此代码

此主函数将向文件写入测试映射,然后将其读回不同的映射,然后比较两者

int main() {
    using std::map; 
    using std::string; 
    map<string, map<string, string>> test_map {
        {"Hello", {{"A", "B"}, {"C", "D"}}},
        {"World", {{"Blarg", "glug glug glug"}, {"idek what to put here", "felt cute might delete later"}}}
    }; 

    {
        auto output_file = fopen("example.txt", "w"); 
        output::write(output_file, test_map); 
        fclose(output_file); 
    }
    map<string, map<string, string>> map_from_file; 
    {
        auto input_file = fopen("example.txt", "r");
        try {
            input::read(input_file, map_from_file); 
        } catch(std::logic_error& err) {
            std::cerr << "Reading example.txt failed: " << err.what() << '\n'; 
        }
        fclose(input_file);  
    }

    std::cout << std::boolalpha << "Maps equivilant? " << (test_map == map_from_file) << '\n'; 


    for(auto pair : map_from_file) {
        std::cout << '"' << pair.first << "\" -> {\n"; 
        for(auto kv : pair.second) {
            std::cout << "  \"" << kv.first << "\" -> \"" << kv.second << '"' << "\n"; 
        }
        std::cout << "}\n"; 
    }
}
intmain(){
使用std::map;
使用std::string;
地图测试{
{“你好”{{“A”,“B”},{“C”,“D”}},
{“世界”{{“布拉格”,“咕噜咕噜咕噜”},{“我想把什么放在这里”,“觉得可爱可能以后删除”}
}; 
{
自动输出_file=fopen(“example.txt”、“w”);
输出::写入(输出文件、测试映射);
fclose(输出文件);
}
从映射文件映射映射;
{
自动输入_file=fopen(“example.txt”、“r”);
试一试{
输入::读取(输入文件,从文件映射);
}捕获(标准::逻辑错误和错误){

STR::CERP为什么你需要从Python生成C++代码?看起来,在Python中做所有的处理或者使用C++中的许多可用JSON库中的一个,并在那里做所有的处理都是更合理的。。即使我将JSON文件转换为更简单的格式,我是否仍然需要创建一些数据结构查找,例如映射,以获得键值对?“我不能有任何外部服务器依赖关系”-@Anaguone下载rapidjson或nlohmann/json没有添加这种依赖关系。你下载一次,然后如果你愿意,可以切断网线。@TedLyngmo,我的意思是,我不能将任何东西下载到我工作的服务器上。为什么?这只是文本文件。如果实施限制的人愿意重新考虑,你可以“你马上就要解决这个问题了。你已经在你的服务器上安装了什么库?你可能已经安装了JSON LIB。如果没有,按照安东尼奥的建议做,把JSON数据转换成一个简单的文件格式,然后你可以在C++中创建一个解析器。@迈肯我更新了这个帖子,给出了一个可以写的例子。以非常简单的文件格式读取地图。
2 5 Hello2 1 A1 B1 C1 D5 World2 5 Blarg14 glug glug glug21 idek what to put here28 felt cute might delete later
namespace output {
    using std::map; 
    using std::string; 
    void write(FILE* file, string const& str) {
        // Write the length of the string, followed by a space
        fprintf(file, "%lu ", str.size());

        // Write the string itself
        fwrite(str.data(), 1, str.size(), file);  
    }

    template<class Key, class Value>
    void write(FILE* file, map<Key, Value> const& m) {
        // Write the length of the map, followed by a space
        fprintf(file, "%lu ", m.size()); 

        for(auto& entry : m) {
            // Write the key
            write(file, entry.first);

            // Write the value
            write(file, entry.second); 
        }
    }
}
namespace input {
    using std::map;
    using std::string; 

    void read(FILE* file, size_t& length) {
        int result = fscanf(file, "%lu ", &length);
        if(result < 0) throw std::logic_error("Couldn't read from file"); 
    }

    void read(FILE* file, string& str) {

        size_t length;      // Read the length
        read(file, length); 

        str.resize(length); 
        size_t n_read = fread(&str[0], 1, length, file); // Read the characters

        if(n_read != length) { // Handle errors
            throw std::logic_error("Unable to read entirety of string from file"); 
        }
    }

    template<class Key, class Value>
    void read(FILE* file, map<Key, Value>& text) {
        size_t length;      // Read the length of the map
        read(file, length); 
        text.clear(); 

        for(size_t i = 0; i < length; i++) {
            Key key;
            read(file, key);        // Read the key
            read(file, text[key]);  // Read the value
        }
    }
}
void write_map(string file, map<string, map<string, string>> test_map) {
    auto output_file = fopen(file.c_str(), "w"); 
    output::write(output_file, test_map); 
    fclose(output_file); 
}
map<string, map<string, string>> read_map(string file) {
    auto input_file = fopen(file.c_str(), "r"); 
    map<string, map<string, string>> m;
    input::read(file, m); 
    fclose(input_file); 
    return m; 
}
int main() {
    using std::map; 
    using std::string; 
    map<string, map<string, string>> test_map {
        {"Hello", {{"A", "B"}, {"C", "D"}}},
        {"World", {{"Blarg", "glug glug glug"}, {"idek what to put here", "felt cute might delete later"}}}
    }; 

    {
        auto output_file = fopen("example.txt", "w"); 
        output::write(output_file, test_map); 
        fclose(output_file); 
    }
    map<string, map<string, string>> map_from_file; 
    {
        auto input_file = fopen("example.txt", "r");
        try {
            input::read(input_file, map_from_file); 
        } catch(std::logic_error& err) {
            std::cerr << "Reading example.txt failed: " << err.what() << '\n'; 
        }
        fclose(input_file);  
    }

    std::cout << std::boolalpha << "Maps equivilant? " << (test_map == map_from_file) << '\n'; 


    for(auto pair : map_from_file) {
        std::cout << '"' << pair.first << "\" -> {\n"; 
        for(auto kv : pair.second) {
            std::cout << "  \"" << kv.first << "\" -> \"" << kv.second << '"' << "\n"; 
        }
        std::cout << "}\n"; 
    }
}