set_intersection of two sets

This is a discussion on set_intersection of two sets within the C++ Programming forums, part of the General Programming Boards category; set_intersection - C++ Reference has an example of finding the set_intersection, but it declares the intersection set as Code: vector<int> ...

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

    set_intersection of two sets

    set_intersection - C++ Reference
    has an example of finding the set_intersection, but it declares the intersection set as
    Code:
     vector<int> v(10);
    why exactly we need to specify the memory, as I know vector is dynamic array, this works fine even when I don't specify for smaller set intersection but when the intersection comes out to be bigger it shows segmentation fault, How to fix it without killing the dynamic property of the vector, or is there any better way to find set intersection, its better for us to use STL library if am not wrong.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    You need to use, say, a back_insert_iterator (use std::back_insert to construct one). Iterators aren't dynamic; they require that there is enough storage.
    Dynamic iterators do exist, and you can find most of them in the <iterator> header.
    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.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134
    Quote Originally Posted by Elysia View Post
    You need to use, say, a back_insert_iterator (use std::back_insert to construct one).
    What is back_insert_iterator, I have used:
    Code:
    vector.push_back(element);
    are you saying about that?

    What do you mean by dynamic iterator could you give me the starting concept?

    why they require enough space, how much space they require?\

    Quote Originally Posted by Elysia View Post
    you can find most of them in the <iterator> header.
    Most of them?? Is there many kind of dynamic Iterators?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    A normal iterator is a glorified pointer. It points to existing elements inside a container. Thus, you can only write to a specific element in a container. You cannot add new elements to a container via a normal iterator. You can only overwrite.
    However, back_insert_iterator is special. What is basically is is an iterator associated with a container, and when you write to such an iterator, it calls push_back on its associated container. So it will add elements.
    There are other such iterators, such as stream iterators.
    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.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    134
    Then why when I used:
    Code:
    vector.push_back(element);
    Its not working?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    What does that have to do with set_intersection?
    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.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Quote Originally Posted by kapil1089thekin View Post
    Then why when I used:
    Code:
    vector.push_back(element);
    Its not working?
    What I find amazing is that you tried to write this statement in the middle of a set_intersection call.

    The iterator basically contains this and calls this->push_back(whatever); on a dereference operation, because it overloaded the operators the algorithm was written with. That's why a back_inserter() works and what you tried does not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of sets
    By kapil1089thekin in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2010, 03:12 PM
  2. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 07:09 PM
  3. Major Problems with GetSaveFileName() and GetOpenFileName()
    By CodeHacker in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2004, 11:05 AM
  4. creating new sets
    By axon in forum C++ Programming
    Replies: 7
    Last Post: 12-03-2003, 05:37 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

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