Results 1 to 5 of 5

Thread: How to check if an int on predefined list of values?

  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to check if an int on predefined list of values?

    Hi everybody. I wonder if in C++ or Qt there is some simple way to check if an int equals to some int from a predefined list. I mean something like pseudo code below. Much thanks.
    Qt Code:
    1. int a = 3;
    2. int b = 8;
    3. int c = 20;
    4.  
    5. int x = 5;
    6.  
    7. if (x in [a,b,c]) {
    8. //further processing
    9. }
    10.  
    11. if (x in [2,7,5]) {
    12. //further processing
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to check if an int on predefined list of values?

    Yes.

    For example, if the list is a QVector, it has a contains() method.
    Or in a QSet if the values are not needed in an ordered fashion.

    Similar for STL containers.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to check if an int on predefined list of values?

    Thanks, I guess I got it, but it still feels awkward, is there some shorter way, please post a short snippet, thanks.
    Qt Code:
    1. QVector<int> vec1(QVector<int>(5));
    2. vec1[0] = 1;
    3. vec1[1] = 2;
    4. qDebug() << "vec1 contains:" << vec1.contains(2);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to check if an int on predefined list of values?

    It depends.

    You can do things like this
    Qt Code:
    1. QVector<int> vec = QVector<int>() << 1 << 2;
    2. QSet<int> set = QSet<int>() << 1 << 2;
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. typedef QVector<int> IntVector;
    2. typdef QSet<int> IntSet;
    3. IntSet vec = IntVector() << 1 << 2;
    4. IntSet set = IntSet() << 1 << 2;
    To copy to clipboard, switch view to plain text mode 
    with C++11 even
    Qt Code:
    1. auto vec = IntVector() << 1 << 2;
    2. auto set = IntSet() << 1 << 2;
    To copy to clipboard, switch view to plain text mode 
    maybe even
    Qt Code:
    1. auto vec = IntVector{ 1, 2 };
    2. auto set = IntSet{ 1, 2 };
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to check if an int on predefined list of values?

    Very good, much thanks

Similar Threads

  1. Replies: 5
    Last Post: 27th November 2014, 11:15
  2. what predefined font styles are given?
    By szisziszilvi in forum Newbie
    Replies: 1
    Last Post: 3rd August 2011, 16:28
  3. Replies: 1
    Last Post: 23rd April 2011, 18:33
  4. Replies: 8
    Last Post: 12th November 2009, 01:51
  5. List withembedded check boxes
    By bruccutler in forum Newbie
    Replies: 3
    Last Post: 7th March 2007, 19:17

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.