473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

STL vector

Airslash
221 100+
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
Expand|Select|Wrap|Line Numbers
  1.     for(std::vector<Drive>::const_iterator it = drives.begin(); it != drives.end(); ++it)
  2.     {
  3. //        if(drives[i].GetDriveLetter() == DriveLetter)
  4.         if(it->GetDriveLetter() == DriveLetter)
  5.         {
  6.             // DEBUG
  7.             OutputDebugStringA(AnsiString("[DriveManager] :: Found a Drive that has DriveLetter = '" + AnsiString(DriveLetter) + "'").c_str());
  8.  
  9.             // We found a drive with the same DriveLetter,
  10.             // set our return value to true.
  11.             return_value = true;
  12.  
  13.             // exit the for loop using a break.
  14.             break;
  15.         }
  16.     }
  17.  
The vector is defined as std::vector<Drive> drives; inside the header of my Singleton class.
Feb 10 '10 #1
4 2416
weaknessforcats
9,208 Expert Mod 8TB
I'm currently using a vector to store objects in my Singleton class.
The vector is defined as std::vector<Drive> drives; inside the header of my Singleton class.
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.
Feb 10 '10 #2
Airslash
221 100+
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:

Expand|Select|Wrap|Line Numbers
  1. drives.push_back(Drive('C'));
  2.  
I can see in the debugger that the drives vector contains a drive object.
If I however want to iterate through the vector at a later point and havent removed any of the drives, the vector reports size 0, but the debugger informs me that it still contains information.
Feb 11 '10 #3
Banfa
9,065 Expert Mod 8TB
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?
Feb 11 '10 #4
Airslash
221 100+
I forgot the assignment overloading.....thanks Banfa for pointing that out.

What I try to say with the data is that in the debugger, I'm able to "drag" my object to the debuggerwatch and browse through it's data members. There I can see all the drives that are added in it.

I've got it working now though, but not really sure how I got to it....think i mixed up a pointer somewhere.
Feb 11 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
16
by: Martin Jørgensen | last post by:
Hi, I get this using g++: main.cpp:9: error: new types may not be defined in a return type main.cpp:9: note: (perhaps a semicolon is missing after the definition of 'vector') main.cpp:9:...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.