Array of sets

This is a discussion on Array of sets within the C++ Programming forums, part of the General Programming Boards category; How to implement array of sets? is there anyway I can do it dynamically, just at the running time I ...

  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
    22,118
    Perhaps you can use a std::vector<std::set<T> >
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,118
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,118
    Post the smallest and simplest program that you expect to compile but which demonstrates the error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,118
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    22,897
    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, 05:23 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11: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, 08:01 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21