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