473,804 Members | 3,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL vector

Airslash
221 New Member
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<Dri ve> drives; inside the header of my Singleton class.
Feb 10 '10 #1
4 2453
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Feb 10 '10 #2
Airslash
221 New Member
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 Recognized Expert Moderator Expert
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 New Member
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
3211
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 "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
9
2977
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 seem to work. I would appreciate someone to comment on this. Version 1 (crashes on deallocating) #include <iostream>
7
10630
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 namespace std; : : vector<string>* getTyphoon()
34
4181
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 "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
10
4849
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
5117
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
16
4441
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: error: two or more data types in declaration of 'set' I don't really see the problem... Here's the code:
23
6553
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: vector<string> tokenize(string s){
6
6831
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 vector<bool>, does each element takes 4 bytes instead of 1 bit? I am using gcc3.4.4. There is a bit_vector which is kind of old so I wont use that. Any other choices? Thanks ahead. zl2k
24
2964
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 vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10320
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.