STL矢量 [英] STL vector

查看:63
本文介绍了STL矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在使用向量来存储我的Singleton类中的对象。

我有一个函数可以检查是否有特定的对象已经在向量内部并且如果它存在则报告为真。


我的for循环然后跳过它。我已经尝试过使用int循环遍历位置,迭代器和size_t但是这三个只是跳过整个向量而不访问元素。


当我看到内存中的向量与调试器我注意到第一次向量是完全空的(因为它应该是它刚刚初始化),但第二次有一个对我的对象的引用,我cna读取它的值和地址通过调试器,但向量报告它的大小为0,迭代器只是跳过它而不是开始和结束地址是不同的。


这是代码

Hello,

I''m currently using a vector to store objects in my Singleton class.
I have a function that checks if a specific object is already inside the vector and reports true if it is present.

my for loop however jump right through it. I''ve tried it with using an int to loop through the locations, an iterator and size_t but all three just jump through the entire vector without accessing the elements.

When I look at the vector in memory with the debugger I noticed that the first time the vector is complelty empty (as it should be since its just initialized), but the second time there is one reference to my object in it, i cna read it values and adress through the debugger, but the vector reports its size as 0 and the iterator just jumps through it althoug the begin and end adress are different.

this is the code

展开 | 选择 | Wrap | 行号

推荐答案


我正在使用向量来存储我的Singleton类中的对象。
I''m currently using a vector to store objects in my Singleton class.


向量定义为std :: vector< Drive>驱动器;在我的Singleton类的标题内。
The vector is defined as std::vector<Drive> drives; inside the header of my Singleton class.



这些是你的报价。


因此,向量有你的单身人士,你的向量是在你的标题中定义的单身人士班。现在这是否意味着你得到一个新的矢量< Drive>每次你包括你的标题?


另外,我没有看到你的矢量应该是一个矢量。我认为你需要一个Register类,该类有向量。然后,创建Singleton并调用Register方法将其添加到向量中。注册需要在匿名命名空间中。


接下来,您应该使用全球可访问的Drive *来为您的单身人士而不是驱动器。


阅读c / C ++见解论坛中有关Singleton设计模式的洞察文章。

These are your quotes.

So, the vector has your Singletons and your vector is defined in the header of your Singleton class. Now does that mean you get a new vector<Drive> every time you include your header?

Also, I don''t see that your vector should be a vector. I think you need a Register class and that class has the vector. Then, you create your Singleton and call a Register method to add it to the vector. The Register need to be in an anonymous namespace.

Next, your should be working with a globally accessible Drive* for your singletons and not a Drive.

Read the insight article on the Singleton design pattern in the c/C++ insights forum.


我有一个名为DriveManager的类,这是一个包含向量作为datamember的Singleton Drive类的几个实例。


Singleton类DriveManager具有在向量中添加和删除驱动器的功能。然而,当我检查其中的元素数量时,向量总是报告大小为0,因为我不想两次添加相同的Drive实例。


Singleton按预期工作,那是不是这里的问题。我遇到的问题是当我这样称呼时:

I have a class called DriveManager, this is a Singleton that has a vector as datamember containing several instances of the Drive class.

The Singleton class DriveManager has functions to add and remove drives in the vector. However the vector always reports size 0 when I check for the amount of elements in it, since I dont want to add the same Drive instance twice.

The Singleton is working as intended, thats not the issue here. The problem I have is when I call this:

展开 | 选择 | Wrap | 行号


我假设您已实施有效的驱动器:: operator =允许向量复制。


当你说向量报告大小为0但是调试器告诉我它仍然包含你在说什么信息的信息时,怎么做你明白了吗?
I assume that you have implementated a valid Drive::operator= to allow the vector to copy.

When you say the vector reports size 0 but the debugger informs me that it still contains information what information are you talking about, how do you get it?


这篇关于STL矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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