insert and retreave for std::set()

Discussion in 'C++' started by Rene Ivon Shamberger, Oct 9, 2012.

  1. Using std::set::insert(), I have stored some data in the container, now I would like to extract the data into another object.inset() method. The problem is that std::set() does not have a retreave() function. How do I go about it?
    void insertData(std::string& data){
    ...
    }
    void someMethod(){
    std::set<std::string> set_obj;
    .....
    // put a lot of elements in the set container
    ....
    // now put one element a the time inside this method
    while(there is data in set_obj){

    someObject.insertData(set_obj...retreave(element+);
    }

    If you know about a website that has examples of how to extract one element at the time from a 'std::set' please do let me know.


    TIA
     
    Rene Ivon Shamberger, Oct 9, 2012
    #1
    1. Advertising

  2. Hello,

    On Oct 9, 10:06 am, Rene Ivon Shamberger <> wrote:
    > Using std::set::insert(), I have stored some data in the container, now Iwould like to extract the data into another object.inset() method. The problem is that std::set() does not have a retreave() function. How do I go about it?


    It's hard to answer your question usefully without knowing more about
    why you are using an std::set. For instance, why don't you just
    insert the strings directly into "someObject" instead of putting them
    in an intermediate std::set? Do you need the automatic sorting and
    uniqueness enforcement it provides? If so, then (depending on what
    "someObject" is), it might be more efficient to instead sort/uniquify
    them with STL algorithms in-place after putting them all into
    "someObject". If you don't need the sorting / uniqueness checks,
    there is no reason to involve the std::set at all.


    > void insertData(std::string& data){
    >  ...}
    >
    > void someMethod(){
    > std::set<std::string> set_obj;
    > ....
    > // put a lot of elements in the set container
    > ...
    > // now put one element a the time inside this method
    > while(there is data in set_obj){
    >
    > someObject.insertData(set_obj...retreave(element+);
    >
    > }
    >
    > If you know about a website that has examples of how to extract one element at the time from a 'std::set' please do let me know.
    >
    > TIA



    If I understand correctly, you want a function that returns an element
    from the set and also removes it from the set at the same time. I
    don't think that std::set has any such member function. I agree that
    this is a bit of a lack; it would be nice to have a function

    T && std::set<T>::extract(std::set<T>::iterator);

    (and similar such functions for other containers where it makes
    sense).

    Lacking that function, you can simulate a loop over the set contents
    that achieves what appears to be your aim with:

    while (! set_obj.empty()) {
    someObject.insertData(* set_obj.begin());
    set_obj.erase(set_obj.begin());
    }

    This won't be super-efficient, as a copy of the object will be made.
    But, since you don't seem to care about the contents of set_obj after
    someObject is filled, you could dispense with the calls to erase() and
    just loop through:

    for (set<string>::const_iterator it = set_obj.begin(),
    e = set_obj.end(); it != e; ++it)
    someObject.insertData(* it);

    or equivalently, if someObject supports a common STL container
    semantic,

    someObject.assign(set_obj.begin(), set_obj.end());

    - Kevin B. McCarty
     
    Kevin McCarty, Oct 9, 2012
    #2
    1. Advertising

  3. Rene Ivon Shamberger

    Jorgen Grahn Guest

    On Tue, 2012-10-09, Rene Ivon Shamberger wrote:
    > Using std::set::insert(), I have stored some data in the container,
    > now I would like to extract the data into another object.inset()
    > method. The problem is that std::set() does not have a retreave()
    > function. How do I go about it?


    This for example would copy your set's elements into a vector
    (and keep the order):

    std::set<Foo> s = something();
    std::vector<Foo> v;
    std::copy(s.begin(), s.end(), std::back_inserter(v));

    But you need to learn *why* it works and how it all fits together, not
    just how to do this one thing. You need to read up on how the standard
    containers in general (they work in pretty much the same way) work,
    and how they interact with C++ iterators.

    (It's worth the effort.)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 9, 2012
    #3
  4. Jorgen Grahn <> wrote:
    > std::set<Foo> s = something();
    > std::vector<Foo> v;
    > std::copy(s.begin(), s.end(), std::back_inserter(v));


    In that particular case I would simply write:

    std::vector<Foo> v(s.begin(), s.end());
     
    Juha Nieminen, Oct 10, 2012
    #4
  5. Christian Gollwitzer <> wrote:
    > std::set<Foo> s = something();
    > std::vector<Foo> v;
    > for (auto element : s) {
    > v.insert(element);
    > }


    I think you really want to use for(auto& element: s)
     
    Juha Nieminen, Oct 10, 2012
    #5
  6. Rene Ivon Shamberger

    Jorgen Grahn Guest

    On Wed, 2012-10-10, Juha Nieminen wrote:
    > Jorgen Grahn <> wrote:
    >> std::set<Foo> s = something();
    >> std::vector<Foo> v;
    >> std::copy(s.begin(), s.end(), std::back_inserter(v));

    >
    > In that particular case I would simply write:
    >
    > std::vector<Foo> v(s.begin(), s.end());


    Ah, yes, of course! My version is only useful if v already contains
    things which you want to preserve, and even then you seem to have
    std::vector::insert(where, first, last).

    It's not that I didn't know about the iterator pair constructor. I use
    it a lot. But it *could* have been that way: it took me several years
    to find it for some reason.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 10, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    Can't retreave new value from Datagrid onUpdate Command

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    503
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

    how can I retreave a key from view state

    =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=, Jul 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    400
    Eliyahu Goldin
    Jul 28, 2005
  3. Peter Jansson
    Replies:
    5
    Views:
    6,391
    Ivan Vecerina
    Mar 17, 2005
  4. Vinu
    Replies:
    4
    Views:
    384
    Jim Langston
    Jul 7, 2005
  5. Neelesh
    Replies:
    5
    Views:
    1,055
    James Kanze
    May 24, 2009
Loading...

Share This Page