Hi all,
I have a Set contain several elements.
I want to do:
(1) Select one element from Set randomly;
(2) Delete this element from Set;
(3) If Set!=empty, goto(1);else, end.
Is there any easy approach?
Best regards,
Robert 6 8632
Robert wrote: I have a Set contain several elements.
What's "Set"? Do you mean 'std::set' or some other implementation?
I want to do: (1) Select one element from Set randomly; (2) Delete this element from Set; (3) If Set!=empty, goto(1);else, end.
Is there any easy approach?
What do you mean by "easy approach"? If I presume 'std::set', then
you can get the number of elements in the set ('size' member), call
'rand' (to get the random number), scale it to the size, get the
set beginning iterator, advance it (using 'std::advance'), then
erase the element pointed to by that iterator. Repeat until empty.
How much easier than that do you need?
BTW, what's the importance of the element erased to be random?
V
Robert wrote: Hi all,
I have a Set contain several elements.
I want to do: (1) Select one element from Set randomly; (2) Delete this element from Set; (3) If Set!=empty, goto(1);else, end.
Is there any easy approach?
Yes: clear the set.
[this instruction has the same pre- and postconditions as your pseudo code]
But, I presume you really want something different, namely you want to keep
track of the order in which the elements are drawn. Here is how you can
achieve an equivalent effect:
Let s be your std::set<T> object.
std::vector<T> deletion_order ( s.begin(), s.end() );
s.clear();
std::random_shuffle( deletion_order.begin(), deletion_order.end() );
Now just pretend that the objects have been deleted in the order represented
by deletion_order.
Best
Kai-Uwe Bux
Robert wrote:
<snip> I have a Set contain several elements.
I want to do: (1) Select one element from Set randomly; (2) Delete this element from Set; (3) If Set!=empty, goto(1);else, end.
Is there any easy approach?
<snip>
If you have the SGI's STL extensions, there's
void random_visit( const std::set<Element> & set,
const Visitor & visitor )
{
std::vector<Element> v;
v.reserve( set.size() );
random_sample_n( set.begin(), set.end(),
std::back_inserter( v ), set.size() );
for_each( v.begin(), v.end(), visitor );
}
Otherwise, you can use copy+random_shuffle:
std::copy( set.begin(), set.end(),
std::back_inserter( v ) );
std::random_shuffle( v.begin(), v.end() );
Marc
"Robert" <zh*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... Hi all,
I have a Set contain several elements.
I want to do: (1) Select one element from Set randomly; (2) Delete this element from Set; (3) If Set!=empty, goto(1);else, end.
Is there any easy approach?
#include <iostream>
#include <iterator>
#include <set>
#include <stdlib.h>
#include <string>
#include <time.h>
void show(const std::set<int>& s, const std::string prefix)
{
std::set<int>::const_iterator it(s.begin());
std::set<int>::const_iterator en(s.end());
std::cout << prefix;
while(it != en)
std::cout << *it++ << '\n';
std::cout.put('\n');
}
int rand_range(int lo, int hi)
{
return lo +
(int)((double)rand() /
((double)RAND_MAX + 1) * (hi - lo + 1));
}
int main()
{
srand((unsigned int)time(0));
std::set<int> s;
for(std::set<int>::size_type i = 0; i < 10; ++i)
s.insert(i);
show(s, "Initial values:\n");
while(!s.empty())
{
std::set<int>::iterator it(s.begin());
std::advance(it, rand_range(0, s.size() - 1));
int i = *it;
s.erase(it);
std::cout << i << " deleted\n\n";
show(s, "Current values:\n");
}
return EXIT_SUCCESS;
}
Of course the end result could be achieved much more
succintly:
s.clear();
-Mike Best regards, Robert
Marc Mutz wrote: If you have the SGI's STL extensions, there's
+ random_sample_n, so you can write
void random_visit( const std::set<Element> & set, const*Visitor*&*visitor*) { std::vector<Element>*v; v.reserve(*set.size()*); random_sample_n(*set.begin(),*set.end(), std::back_inserter(*v*),*set.size()*); for_each(*v.begin(),*v.end(),*visitor*); }
Marc,
love-hates CTRL-K
Thank you all for your help!
Best regards,
Robert This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Joe Six-Pack |
last post by:
Hi,
Im having problems in randomly selecting an element in an array that
has not been selected before.. in other words, I have an array of
answers to questions, then I want to select 5, with one...
|
by: Bob Bedford |
last post by:
I've a site where companies add their article.
I'de like to provide a "lasts articles" table. By this, I'll show last
articles inserted. But I won't always the same articles at any refresh....
|
by: Davy |
last post by:
Hi all,
I have a Set contain several elements.
I want to do:
(1) Select one element from Set randomly;
(2) Delete this element from Set;
(3) If Set!=empty, goto(1);else, end.
Is there any...
|
by: ina |
last post by:
Hello all,
I am newbie in xml and have a problem with this parse.
I have this xml.file
<Style>
<Strategy>
|
by: fran7 |
last post by:
Hi, I have this code to randomly select an image from a sequence of fields in my database. Does anyone know if rather than random, one could simply move from one field to the next sequentialy. That...
|
by: tamaker |
last post by:
Is this do-able with ASP / VBscript?
-- I have a database with user records (name, photo, etc. etc.)
I want to use asp to generate (on the homepage) a series of 4 randomly
selected 'user...
|
by: caca |
last post by:
Hello,
This is a question for the best method (in terms of performance
only) to choose a random element from a list among those that satisfy
a certain property.
This is the setting: I need to...
|
by: Patrick Nolan |
last post by:
I am using javascript to manipulate optgroups in select elements.
When the results are displayed in Firefox 2.0 there is an
annoying blank line at the top of the multi-line select box.
This...
|
by: ezechiel |
last post by:
I've been looking for this on the forum, but didn't find anything in relation with excel.
So, I have cells with text, from A3 -> A159
And I need to select 50% (or 25% or 12.5%) of these cells...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |