简体   繁体   English

使用向量

[英]Working with Vectors

I am a student and haven't been taught vectors yet, but I need to use them now. 我是一个学生,还没有被教过向量,但是我现在需要使用它们。 I am trying to make a vector of structs. 我正在尝试构建结构向量。 The struct is a label for assembly code. 该结构是汇编代码的标签。 I resize the vector each time I want to add a label, however, when I try to retrieve the information from the labels, they are all the same. 我每次想添加标签时都会调整矢量的大小,但是,当我尝试从标签中检索信息时,它们都是一样的。 I can't actually share my code unfortunately, but I will show what I think are the lines necessary to help me solve this problem. 不幸的是,我实际上不能共享我的代码,但是我将展示我认为帮助我解决此问题的必要行。 So, what am I doing wrong? 那么,我在做什么错呢? If you need to see more code, let me know. 如果您需要查看更多代码,请告诉我。

Declared globally: 全球宣布:

vector<Label> labeldir;

Resizing vector and adding information, label is the name of a label in a file of assembly code: 调整向量大小并添加信息,label是汇编代码文件中标签的名称:

labeldir.resize(labelnum + 1);
labeldir[labelnum].name = label;

Note: it is running in a while loop, labelnum is incrimented each time a label is found. 注意:它在while循环中运行,每次找到标签时都会增加labelnum。

Example of accessing names of each Label: 访问每个标签名称的示例:

for (int i = 0; i < labeldir.size(); i++){
    cout << "Label entry " << i << " name : " << labeldir[i].name << endl;
}

The corresponding output, notice the issue (the above code is in a while loop, this is 3 iterations of it): 相应的输出,请注意问题(上面的代码在while循环中,这是它的3次迭代):

Label entry 0 name : start
Label entry 0 name : done
Label entry 1 name : done
Label entry 0 name : five
Label entry 1 name : five
Label entry 2 name : five

The first 3 labels in the assembly code, there would be lines between the first and second label but they would be regular instructions.: 汇编代码中的前3个标签,在第一个和第二个标签之间会有一行,但是它们是常规说明。

start   add 1   2   1   decrement reg1
done    halt                end of program
five    .fill   5

It's a bit hard to see exactly what you're doing wrong but let me have a guess: 确切地知道您在做什么错有点困难,但让我猜测一下:

Your code to load up your vector should probably look like this: 您用于加载向量的代码可能如下所示:

#include <string>

struct Label
{
    Label(const std::string& N) : Name(N)
    {}

    Label()
    {}

    std::string Name;
};

int main()
{
    std::vector<Label> labeldir;

    labeldir.push_back(Label("one"));
    labeldir.push_back(Label("two"));
    labeldir.push_back(Label("three"));
    labeldir.push_back(Label("four"));

    int i = 0;
    for(auto l : labeldir)
    {
        std::cout << "Label entry " << i++ << " name : " << l.Name << "\n";
    }

    system("PAUSE");
    return 0;
}

I'd almost bet that the name member of your Label structure is a char * . 我几乎敢打赌,您Label结构的name成员是char * That means instead of creating a separate string for each label, you're only creating separate pointers -- but they're all pointing to the same place. 这意味着您不必为每个标签创建单独的字符串,而只是创建单独的指针-但它们都指向同一位置。

When you try to print out your contents, they're all pointing to the same buffer, so every Label shows the name most recently read into that buffer. 当您尝试打印内容时,它们都指向同一缓冲区,因此每个Label都显示最近读入该缓冲区的名称。

Cure: use std::string instead of char * . 治愈:使用std::string代替char *

From the information that you have provided i am assuming that you are having a problem in adding label names to vector labeldir. 根据您提供的信息,我假设您在向矢量labeldir添加标签名称时遇到问题。

whenever you found a label you can add in the following way: 只要找到标签,就可以通过以下方式添加:

vector<Label> laberdir;
//As you fill the struct label with the name
Label label1;
label1.name = "start";
labeldir.pushback(label1);
labelNum = labelNum + 1;

and as you sad Label is of struct type as 和你伤心的标签是结构类型为

struct Label
{
     std::string name;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM