c++ - 使用指针c++的问题

标签 c++ arrays string pointers char

#include <iostream>
#include <fstream>
#include <cstring>
#include <map>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    //define a bool to determine if you're still in the file's header section or not
    bool header = true;
    //define the map as a multidimensional string array that stores up to 100 z-levels
    //and 50x50 tiles per z level
    char* worldmap[100][50][50];
    int zLevel=0;
    //header declaration map
    map<string, char*> declarations;
    //create an input file stream object
    ifstream file;
    //open file
    file.open("map1.dmm");
    //validate file
    if(!file.good()){return 1;}

    //begin reading the file
    while(!file.eof()){
        //create a character array to write to
        char line[512];
        //read the file line by line; write each line to the character array defined above
        file.getline(line, 512);

        //header check
        if(header){
            if(!line[0]){
                header = false;
                break;
            }else{
                bool declaringKey=true;
                char* key={};
                char* element={};
                char* token[20]={};
                token[0] = strtok(line, "\"()");
                for(unsigned int n = 0;n<20;n++){

                    if(n>0)token[n] = strtok(NULL, "\"()");
                    //cout << token[0] << endl;
                    if(!token[n] || (token[n])[1] == '=')continue;
                    if(declaringKey){

                        key = token[n];
                        declaringKey=false;

                    }else{
                        //cout << "pow" <<endl;
                        element = token[n];
                        cout<<"declarations[" << key << "] = " << element << endl;
                        declarations.emplace(key,element); //<-------------- problem line, i think
                        cout << declarations[key] << endl;

                    }

                }declaringKey=true;

            }
        }else{
            if(!line[0]) {
                zLevel++;
                continue;
            }

        }

    }
//string test = "aa";

    return 0;

}

我正在尝试创建一个 map 加载器,用于从文本文件加载简单 map 。我知道还有其他可用的 map 加载器,但其中大多数的功能远远超出我的需要。到目前为止,这段代码只读取了头部,它基本上定义了每组字符代表什么作为一个瓦片,例如:“aa”=“沙瓦”

问题是,当我将键/元素放置到声明映射中时,它似乎对所有键使用相同的元素。我假设这是因为通过定义一个字符指针,它总是指向相同的数据,并且仅用于更改该指针包含的值的目的,而不是分配新数据并将它们分开。

但这又提出了另一个问题,为什么它会为相同的元素放置不同的键,即使两者都是指针......?总之,

我怎样才能使所有的键/元素都是独立的字符数组,而不是指向由数组分割出的完全相同的空间的指针..?

编辑:除了将相同元素存储到所有键之外,您可以假设代码有效。此外,它存储的元素始终是从文件的 header 部分解析出来的最后一个元素。

最佳答案

也只需使用 std::string 作为值。这应该可以解决您眼前的问题。

就是说,不要不要使用stream.eof() 来控制循环读取值!这是行不通的。相反,总是尝试从流中读取,然后验证读取是否成功,例如:

while (file.getline(line, sizeof(line))) {
    // ...
}

就个人而言,我不会读入固定大小的缓冲区并使用 std::string 代替:

for (std::string line; std::getline(file, line); ) {
    // ...
}

从这一点来看,我也不会使用 strtok(),而是使用 std::string 的成员或合适的算法。这样我也不会误入歧途,考虑到存储指针是个好主意(更不用说我无法处理指针,因此我的程序不使用它们)。

关于c++ - 使用指针c++的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20293391/

相关文章:

c++ - 如何在 C++ 中重用字符串变量

C++:从 x509v3 证书中检索主题替代名称

C++ 对象的映射顺序

arrays - swift 3 数组太长,太复杂,无法解决

java - 在数组中存储随机数

c# - 按 2 个小数点对字符串列表进行排序

c++ - boost 图 : Are add_edge and remove_edge thread safe?

C++:在最后优化时计算零

javascript - 在数组上迭代数组而不重复

python - 将字典中没有的单词替换为