Results 1 to 18 of 18

Thread: What support for STL sets?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default What support for STL sets?

    Hello,
    I need to store ordered sets of various types so I thought STL sets would be just the thing, so I wrote a test programme that began like this:

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <set>
    3. #include <iterator>
    4. #include <iostream>
    5. using std::cout;
    6. using std::endl;
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11. set<int> intSet1;
    12. // more follows
    To copy to clipboard, switch view to plain text mode 

    Unfortunately the compiler tells me that "set was not declared in this scope".

    Is STL not supported, or have I made a mistake?
    I'm using QTCreator++ 1.3.0 with MinGW in Windows XP

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

    Default Re: What support for STL sets?

    set is part of the std namespace; you need to refer to it as std::set, or work around it with a 'using' statement as you have for cout and cerr. I'd recommend explicitly qualifying it with std::set, given the commonality of the name.

  3. #3
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What support for STL sets?

    Thankyou,
    My programme works now, and I appreciate the advice concerning the systematic use of std::set.

  4. #4
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What support for STL sets?

    OK my programme with set<int> was just a warm-up exercise.
    What I'd like to do now is create a set whose comparison criterion is based on a Function object, (aka a Functor).
    The aim is to compare two dimensional points by comparing their distance from a base point, The base point is something that is going to be moving in the program, so this is implemented as a private field m_basepoint of a class.
    The comparison is implemented by overloading the () operator something like this:
    Qt Code:
    1. bool operator()(QPointF p1,QPointF p2){return distance(m_basepoint,p1) < distance(m_basepoint,p2);}
    To copy to clipboard, switch view to plain text mode 
    (where distance is an obvious distance function) and putting this in a class CompareToBase
    Unfortunately
    it seems that
    Qt Code:
    1. set<Type, Functor>
    To copy to clipboard, switch view to plain text mode 
    is not foreseen in STL.
    also
    Qt Code:
    1. set<QPointF,CompareToBase>
    To copy to clipboard, switch view to plain text mode 
    has a problem: how do you set the base_point?
    Last edited by feraudyh; 12th May 2010 at 18:52.

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: What support for STL sets?

    Your operator() should return bool.

  6. #6
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What support for STL sets?

    Yes, that was a typo. I fixed it, thanks.
    Last edited by feraudyh; 12th May 2010 at 18:52.

  7. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: What support for STL sets?

    What is the issue then? You don't know how to initialize the set?
    Qt Code:
    1. set<QPointF,CompareToBase> S(a, a+N); //where a is OPointF array of N elements // or for other containers use iterators .begin(), .end()
    To copy to clipboard, switch view to plain text mode 

    LE: Documentation for set

  8. #8
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What support for STL sets?

    Well, the issue is how to set the "memory" or internal state of the comparison, i.e. the value of m_basepoint.
    It would be nice if I could set this though set initialisation, but I didnt know i could, I will have another look at the documentation for set.
    I suppose you mean the SGI documentation.
    Last edited by feraudyh; 12th May 2010 at 19:39.

  9. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: What support for STL sets?

    m_basepoint has nothing to do with the set, isn't it in the implementation of the class that will be contained in the set? And i recommend to initialize all objects that you create, especially if you want to put them in a std::set (witch must compare objects, so: you can't add uninitialized objects to a set)

    And, be careful with set, because it can contain an object only once, if this is a problem then you should look for std::multiset. Choose your containers with care.

    The SGI is an implementation of STL, STL itself is only a paper, just like C++ standard, but the container interface should be the same in SGI implementation, GCC, QT, or Microsoft (i think this is done be Dinkumware) or anybody-else who implement the STL...

  10. #10
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What support for STL sets?

    Yes, m_compare has nothing to do with the set, if we are thinking of mathematical object but it does have an impact on the ordering. You see these sets are not just mathematical sets but they are ordered using a binary tree.
    For me the order is very important.

  11. #11
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: What support for STL sets?

    For me the order is very important.
    That is why you need to have the elements initialized before you can add them to std::set, or std::multiset.

    The set can't order your objects if they don't have a value, so the data member must have a value so that set can sort your objects.
    The set doesn't know anything about your data, it just calls the CompareToBase functor, witch must be able to tell if one object is smaller (or not) then the other objects that are in set.

    So: you need to have the objects created and initialized (read them form keyboard or from file etc) and then you put them in set/multiset (witch will sort them).

    Or, if you are not comfortable yet with set/multiset, use an c-style array of your data type and sort it with std::sort like this:
    Qt Code:
    1. std::sort(ar, ar+N, CompareToBase); // ar is the array, N is array size, and your CompareToBase functor
    To copy to clipboard, switch view to plain text mode 
    or an std::vector, like this:
    Qt Code:
    1. std::sort(vec.begin(), vec.end(), CompareToBase); // vec is std::vector<your_data_type>
    To copy to clipboard, switch view to plain text mode 
    You need: #include <algorithm> if you use std::sort, anyway you need your data to be initialized, and vector and array don't sort automatically as you add elements.

  12. #12
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What support for STL sets?

    Ok, let's suppose my operator returns bool as I should have written.
    Somehow going through the STL documentation, I have this feeliing I am going to have to pull out tricks like the use of
    bind2nd or something concerning adaptable functors....
    Am I looking in the right direction?
    All of this brings to mind the Lambda Calculus in disguise.

Similar Threads

  1. DvB Chip sets
    By yuvaraj.yadav in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 29th January 2010, 05:16
  2. Replies: 8
    Last Post: 8th May 2009, 09:14
  3. Creating images from sets of data.
    By maverick_pol in forum Qt Programming
    Replies: 5
    Last Post: 26th February 2008, 09:25
  4. QT Nested Sets SQL/XML - categories (tree)
    By patrik08 in forum Newbie
    Replies: 6
    Last Post: 13th June 2006, 00:17

Tags for this Thread

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.