使用向量自动分配对象名称 [英] Using vectors to automatically assign object names

查看:92
本文介绍了使用向量自动分配对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以这需要一些解释,但我是C ++的新手,所以请耐心等待。



我想要实现的目标我的程序是让用户添加具有某些属性的项目(这些是我的类变量)。每次用户添加另一个项目时,我希望程序以项目[#] 的格式分配另一个对象,其中#从0开始并从那里开始上升。我一直试图弄清楚向量,从我到目前为止看到的,我将添加一个向量向量< int>项; 。当用户添加另一个项目时,我希望程序向向量添加另一个变量 item#(或类似的,我不太确定)。然后向量将一个对象分配给类'item',用户将在其中添加属性。



再次,我不确定我究竟有多好地解释了这一点,所以如果我做了一个令人难以置信的工作,请告诉我为什么我这样做而不是贬低和说什么,因为我需要能够在这里实际提问:)



编辑:以下是我正在尝试做的一个例子

OK, so this will take a bit of explaining, but I'm new to C++, so bear with me.

What I am trying to accomplish in my program is to have the user add an 'item' with certain attributes (these are my class variables). Every time the user adds another 'item', I want the program to assign another object in the format item[#], where # starts from 0 and goes up from there. I have been trying to figure out vectors, and from what I have seen so far, I'll add a vector vector<int> item;. When the user adds another item, I want the program to add another variable item# (or similar, I am not quite sure) to the vector. The vector will then assign an object to the class 'item', where the user will go through the process of adding the attributes.

Again, I'm not sure exactly how well I explained this, so if I did a HORRIBLE job of this, please tell me why I did so instead of downvoting and saying nothing, as I need to be able to actually ask questions here :)

Here is an example of what I'm trying to do

#include <iostream>
#include <vector>
using namespace std;

class item
{
    public:
        string name;
        string icon;
        //etc.
}

int main()
{
    vector<int> itemVec;
    itemVec.insert(itemVec.end, #) //# will be a variable containing the number of items created by user
    item itemVec[#] //# would be the latest vector item created
    //other content

    return 0;
}





有一个类'item'包含某些细节。

当用户创建一个新项目,我希望以item#格式创建一个新对象,其中#是项目序列中的下一个数字(从0开始)

我正在考虑使用一个矢量来帮助这个过程。如果我没有正确使用它,请告诉我:)

ELI5,请。



什么对我有用



好​​吧,经过一个多小时的热烈讨论后,这些精彩的人终于把它钻进我的思维头骨中了的std ::矢量<类型> v 不必是 int string 或其他任何内容。它可以用于一个类。 @Phillippe Mori终于告诉了我它是怎么回事:



There is a class 'item' with certain details.
When the user creates a new item, I wish for a new object to be created in the format 'item#', where # is the next number in the sequence of items (starting at 0)
I am considering using a vector to help in this process. If I didn't use it right, please let me know :)
ELI5, please.

What worked for me

OK, so after many an hour of heated discussion, these wonderful people finally drilled it into my think skull that the type in std::vector<type> v did not have to be int or string or anything. It can be used for a class. @Phillippe Mori was the one that finally got through to me that that was how it went:

item my_item; // variable used to create items
vector<item> items; // will hold all added items
items.push_back(my_item); // add item 0
items.push_back(my_item); // add item 1
items.push_back(my_item); // add item 2

string item1name = item[1].name; // Example of accessing item #1



谢谢,全部!

- Anti-Antidote



我的尝试:



我一直在寻找答案,但似乎没有答案。数组不起作用,因为我需要它才能扩展以满足用户的需求。


Thanks, all!
- Anti-Antidote

What I have tried:

I have been searching for an answer for this, but no where seems to have the answer. Arrays don't work, as I need it to be able to expand to meet the user's needs.

推荐答案

你很可能需要比<$ c $更好的向量c> vector< int>< / int> ,因为您需要存储对象的详细信息而不仅仅是其索引。用户在请求中提供了哪些信息?如果它是类名,那么您可以立即创建该类的对象并将其添加到向量中,并提供一些其他信息以准确识别它是什么类。也许其他一些集合类型比简单的向量更好,或者你可以使用对结构向量中的[ ^ ]。我认为你的问题需要更详细一些关于这些项目的确切内容。
You most likely need a better vector than vector<int></int>, since you need to store details of the object rather than just its index. What information does the user provide on the request? If it is a class name then you can immediately create an object of that class and add that to the vector, with some additional information to identify exactly what class it is. Perhaps some other collection type would be better than a simple vector, or you could use the pair Structure[^] in the vector. I think your question needs a bit more detail as to exactly what these 'items' are.


这篇关于使用向量自动分配对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆