Results 1 to 7 of 7

Thread: how to get the info if a qvector element has been set?

  1. #1
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to get the info if a qvector element has been set?

    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:
    Qt Code:
    1. QVector<double> v(10);
    2. for(int i = 0; i< 5; i++)
    3. {
    4. v[i] = i;
    5. }
    6. // v[5] and v[6] will be = 0
    7. for(int i = 7; i< 10; i++)
    8. {
    9. v[i] = i;
    10. }
    11. for(int i = 0; i< 10; i++)
    12. {
    13. std::cout << v.at(i) ; // 0123450089
    14. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Szilvi

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get the info if a qvector element has been set?

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

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to get the info if a qvector element has been set?

    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.
    Qt Code:
    1. QVector<*double> v(10);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: how to get the info if a qvector element has been set?

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to get the info if a qvector element has been set?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get the info if a qvector element has been set?

    Quote Originally Posted by FelixB View Post
    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?
    Last edited by szisziszilvi; 15th June 2011 at 07:41.
    Szilvi

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to get the info if a qvector element has been set?

    A simple example of what i mean, plain old C style nothing much of C++,
    Qt Code:
    1. double v[10];
    2. QVector<double*> pv(10);
    3.  
    4. for(int i = 0; i < pv.size(); i++)
    5. {
    6. if((i == 5) or (i == 6)) //no 6th and 7th elements
    7. pv = 0;
    8. else
    9. pv = &v[i];
    10. }
    11.  
    12. for(i = 0; i < pv.size(); i++)
    13. {
    14. if(pv[i])
    15. std::cout << *(pv.at(i)) ; //01234789
    16. }
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by Santosh Reddy; 15th June 2011 at 08:30. Reason: updated contents

Similar Threads

  1. [QML] Video element
    By grezly in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2011, 17:26
  2. casting qvector element
    By rivci in forum Newbie
    Replies: 1
    Last Post: 4th April 2011, 13:58
  3. Remove element of xml
    By VitaliBR in forum Newbie
    Replies: 17
    Last Post: 16th February 2011, 13:28
  4. Help with QML Pathview element
    By chetu1984 in forum Newbie
    Replies: 0
    Last Post: 7th February 2011, 04:49
  5. Can I translate data with GUI element.
    By mourad in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2008, 09:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.