Thread: Array of sets

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Array of sets

    How to implement array of sets? is there anyway I can do it dynamically, just at the running time I wont be able to know how many sets I need but as I create a set i want to save it and then compare it for my purpose, the question is:

    How Can i create sets at the run time without wasting memory, and How can I track them again.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you can use a std::vector<std::set<T> >
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134
    Why this declaration is not working?
    Code:
            vector <set> s;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kapil1089thekin
    Why this declaration is not working?
    Maybe the appropriate using declarations (using std::vector; using std::set) or using directive (using namespace std; ) are/is not in scope.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    134
    I have used
    Code:
    using namespace std;
    And I feel its in the scope, tomake it more clear I posting the error.

    Code:
    rectangle.cpp:230: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
    rectangle.cpp:230: error:   expected a type, got ‘set’
    rectangle.cpp:230: error: template argument 2 is invalid
    rectangle.cpp:230: error: invalid type in declaration before ‘;’ token

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest program that you expect to compile but which demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    134
    Code:
    #include <iostream>
    #include <vector>
    #include <set>
    using namespace std;
    
    int main()
    {
            vector<set> vector_set
            return 0;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The set must be a set of something, e.g., a set of ints:
    Code:
    #include <iostream>
    #include <vector>
    #include <set>
    using namespace std;
    
    int main()
    {
            vector<set<int> > vector_set;
            return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    134

    how to print/access the 1st element of the first set of the vector

    Code:
    #include <iostream>
    #include <vector>
    #include <set>
    using namespace std;
    void add123(vector<set<int> >& numbers);
    
    int main()
    {
            vector<set<int> > vector_set;
            add123(vector_set);
            //cout << vector_set[0]; //how to print/access the 1st element of the first set of the vector vector_set?
            return 0;
    }
    void add123(vector<set<int> >& numbers)
    {
            set<int> s;
            s.insert(123);
            numbers.push_back(s);
    }

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps
    Code:
    #include <iostream>
    #include <vector>
    #include <set>
    using namespace std;
    void add123(vector<set<int> >& numbers);
    
    int main()
    {
            vector<set<int> > vector_set;
            add123(vector_set);
            cout << *vector_set[0].begin();
            return 0;
    }
    void add123(vector<set<int> >& numbers)
    {
            set<int> s;
            s.insert(123);
            numbers.push_back(s);
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 08:09 PM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM