STL vector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Airslash
    New Member
    • Nov 2007
    • 221

    STL vector

    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
    Code:
    	for(std::vector<Drive>::const_iterator it = drives.begin(); it != drives.end(); ++it)
    	{
    //		if(drives[i].GetDriveLetter() == DriveLetter)
        	if(it->GetDriveLetter() == DriveLetter)
    		{
    			// DEBUG
    			OutputDebugStringA(AnsiString("[DriveManager] :: Found a Drive that has DriveLetter = '" + AnsiString(DriveLetter) + "'").c_str());
    
    			// We found a drive with the same DriveLetter,
    			// set our return value to true.
    			return_value = true;
    
    			// exit the for loop using a break.
    			break;
    		}
    	}
    The vector is defined as std::vector<Dri ve> drives; inside the header of my Singleton class.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I'm currently using a vector to store objects in my Singleton class.
    The vector is defined as std::vector<Dri ve> 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.

    Comment

    • Airslash
      New Member
      • Nov 2007
      • 221

      #3
      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:

      Code:
      drives.push_back(Drive('C'));
      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.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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?

        Comment

        • Airslash
          New Member
          • Nov 2007
          • 221

          #5
          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.

          Comment

          Working...