PDA

View Full Version : how to get the info if a qvector element has been set?



szisziszilvi
14th June 2011, 16:01
Hi,

I would like to use a QVector<double> for storing some data. The problem is that there is no guarantee that there will be data given for each possible entry, but the class QVector has 0 as the default value which would be actually a possible one (so would pretend as being a real measurement instead of showing clearly there is a missing data). This is it:


QVector<double> v(10);
for(int i = 0; i< 5; i++)
{
v[i] = i;
}
// v[5] and v[6] will be = 0
for(int i = 7; i< 10; i++)
{
v[i] = i;
}
for(int i = 0; i< 10; i++)
{
std::cout << v.at(i) ; // 0123450089
}


I don't want to use self-designed struct-s or store the boolean information if a real value was given or not in another array, but it would be nice if I could set my own fake value to change this 0 say to -999. This would also be a goal not to create a derived class just for this issue because the class in which I use this QVector should be normally used with as less extra documentation as possible. So it would be a bad habit telling one things like "for adding data use myVector class" or "before adding data make it sure you used the also provided 'cleanVector(double fakevalue)' function"). Something like "addData(QVector)" is straightforward enough to be used easily.

FelixB
14th June 2011, 16:09
have a look at the constructors of QVector. If I understand your issue correctly, you'll find an appropriate one ;)

why do you have to use a qvector? when you have several non-used elements, maybe a diofferent type would fit better...

Santosh Reddy
14th June 2011, 17:14
I don't think any standard container will satisfy your requiremnt, of missing elements (unless you use pointers, instead of values)

If possible try storing pointers to double, and set the elements with no data to 0, and check the element before using it.

QVector<*double> v(10);

SixDegrees
14th June 2011, 18:40
If the values stored range over the entire span of doubles, then the only thing you can do is store some other type that keeps track of set/unset status. Pointers, as already noted, are a good choice. Or you can create a wrapper class around a double to track such things.

More likely, though, your data will occur over a more constrained range, and you can simply assign some value outside that range (say, -9999999.9) to represent 'unset'.

You could also keep a parallel array of bools, but that's a messy solution.

wysota
14th June 2011, 23:10
I think there should be something like DBL_MAX constant that you can use for a "not-a-number" marker. NAN constant could also work. They should be defined either in math.h or float.h.

szisziszilvi
15th June 2011, 08:41
have a look at the constructors of QVector. If I understand your issue correctly, you'll find an appropriate one ;)
haha. funny. :) I think for my purposes this will be the porper solution.

why do you have to use a qvector? when you have several non-used elements, maybe a diofferent type would fit better...
Well, it depends on the purposes.

@Santosh Reddy:

If possible try storing pointers to double
altough this might be no qt-question (rather general programming) but honestly unfortunatelly I don't see the point. Could you please provide me a little bit more hints to see what to read after?

Santosh Reddy
15th June 2011, 09:29
A simple example of what i mean, plain old C style nothing much of C++,

double v[10];
QVector<double*> pv(10);

for(int i = 0; i < pv.size(); i++)
{
if((i == 5) or (i == 6)) //no 6th and 7th elements
pv = 0;
else
pv = &v[i];
}

for(i = 0; i < pv.size(); i++)
{
if(pv[i])
std::cout << *(pv.at(i)) ; //01234789
}

This is very basic example. I am not very clear of you actual requirement behind this, but I understand you need a container which is capable to indicate not used elements. Unlike other programing languages (mostly scripting based), which have something line "nothing" / "not initialized" for not set variables, C / C++ does not have anything like this. One alternate way is to use default ctor to set a "not initialized" value (which in many cases is still a valid value, 0 in your case).

I see you might end-up having your own element class (not container class), or doing something like this example, or as already suggested using some other default value (-99999, NAN) other than 0.