insert and retreave for std::set()

  • Thread starter Rene Ivon Shamberger
  • Start date
R

Rene Ivon Shamberger

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
 
K

Kevin McCarty

Hello,

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
 
J

Jorgen Grahn

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
 
J

Juha Nieminen

Jorgen Grahn said:
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());
 
J

Juha Nieminen

Christian Gollwitzer said:
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)
 
J

Jorgen Grahn

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top