473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

STL and combinations question

Thank you for reading my plea for help.

What I'm trying to achieve is to get the subsets from a given set using the
Standard Template Library.

Say, for example, I have created an empty set and then have inserted 3
numbers into that set (1, 2, 3). Next, I would like to get all of the subset
pairs (1, 2), (1, 3) and (2, 3) for processing.

A Google search has given me a lot to think about but nothing specific for
my needs. I have played with next_permutation and read about
next_combination which sounded more promising, since combinations are what
I'm looking for, but next_combination doesn't appear to be part of STL (572:
error: 'next_combination' was not declared in this scope).

Can someone please put me on the right track? Perhaps a simple example might
help.

--
Regards,
Phil.
Dec 29 '06 #1
5 8134
"Phil" <ph*****@telstra.comwrote in message
news:Tm****************@news-server.bigpond.net.au
Thank you for reading my plea for help.

What I'm trying to achieve is to get the subsets from a given set
using the Standard Template Library.

Say, for example, I have created an empty set and then have inserted 3
numbers into that set (1, 2, 3). Next, I would like to get all of the
subset pairs (1, 2), (1, 3) and (2, 3) for processing.

A Google search has given me a lot to think about but nothing
specific for my needs. I have played with next_permutation and read
about next_combination which sounded more promising, since
combinations are what I'm looking for, but next_combination doesn't
appear to be part of STL (572: error: 'next_combination' was not
declared in this scope).
Can someone please put me on the right track? Perhaps a simple
example might help.

Algorithms for finding all subsets are not trivial and have nothing
specifically to do with the C++ language. Try

comp.programming

--
John Carson
Dec 29 '06 #2
On 2006-12-29 03:49, Phil wrote:
Thank you for reading my plea for help.

What I'm trying to achieve is to get the subsets from a given set using the
Standard Template Library.

Say, for example, I have created an empty set and then have inserted 3
numbers into that set (1, 2, 3). Next, I would like to get all of the subset
pairs (1, 2), (1, 3) and (2, 3) for processing.
This can probably be done using any container but I find a vector
particularly useful in this case:

#include <vector>
#include <utils>

std::vector<intset;
std::vector<std::pair<int, int pairs;

// initialize the set somehow

for (size_t i = 0; i < set.size(); ++i)
for (size_t j = i + 1; j < set.size(); ++k)
pairs.push_back(std::make_pair(i, j));

This will run in about O((n^2) / 2).

--
Erik Wikström
Dec 30 '06 #3
Erik Wikström wrote:
On 2006-12-29 03:49, Phil wrote:
>Thank you for reading my plea for help.

What I'm trying to achieve is to get the subsets from a given set
using the Standard Template Library.

Say, for example, I have created an empty set and then have inserted 3
numbers into that set (1, 2, 3). Next, I would like to get all of the
subset pairs (1, 2), (1, 3) and (2, 3) for processing.

This can probably be done using any container but I find a vector
particularly useful in this case:

#include <vector>
#include <utils>

std::vector<intset;
std::vector<std::pair<int, int pairs;

// initialize the set somehow

for (size_t i = 0; i < set.size(); ++i)
for (size_t j = i + 1; j < set.size(); ++k)
pairs.push_back(std::make_pair(i, j));

This will run in about O((n^2) / 2).

Would boost's "next_combination" work ?

see this:
http://www.bxmy.org/code/combination.hpp
Dec 30 '06 #4
On 2006-12-30 23:06, Gianni Mariani wrote:
Erik Wikström wrote:
>On 2006-12-29 03:49, Phil wrote:
>>Thank you for reading my plea for help.

What I'm trying to achieve is to get the subsets from a given set
using the Standard Template Library.

Say, for example, I have created an empty set and then have inserted 3
numbers into that set (1, 2, 3). Next, I would like to get all of the
subset pairs (1, 2), (1, 3) and (2, 3) for processing.

This can probably be done using any container but I find a vector
particularly useful in this case:

#include <vector>
#include <utils>

std::vector<intset;
std::vector<std::pair<int, int pairs;

// initialize the set somehow

for (size_t i = 0; i < set.size(); ++i)
for (size_t j = i + 1; j < set.size(); ++k)
pairs.push_back(std::make_pair(i, j));

This will run in about O((n^2) / 2).


Would boost's "next_combination" work ?

see this:
http://www.bxmy.org/code/combination.hpp
The link does not lead to code that is part of boost, and from the code
I saw it looked unnecessarily complicated, but it might work. Best way
to find out is to test it. You might also be interested in the
following: http://www.codeproject.com/cpp/CombC.asp

--
Erik Wikström
Dec 31 '06 #5
In article <Tm****************@news-server.bigpond.net.au>,
"Phil" <ph*****@telstra.comwrote:
Thank you for reading my plea for help.

What I'm trying to achieve is to get the subsets from a given set using the
Standard Template Library.

Say, for example, I have created an empty set and then have inserted 3
numbers into that set (1, 2, 3). Next, I would like to get all of the subset
pairs (1, 2), (1, 3) and (2, 3) for processing.

A Google search has given me a lot to think about but nothing specific for
my needs. I have played with next_permutation and read about
next_combination which sounded more promising, since combinations are what
I'm looking for, but next_combination doesn't appear to be part of STL (572:
error: 'next_combination' was not declared in this scope).

Can someone please put me on the right track? Perhaps a simple example might
help.
This question comes up often enough that I believe C++ should have a
standard solution. I do not find a need for this functionality often.
But I do need it sometimes. And when I need it, I really need it badly.
And it is very non-trivial to get right.

I most recently used the below-linked code to semi-automate the
generation of test cases for the rvalue reference work. I needed to
test all combinations of function calls involving lvalue and rvalue
arguments to overload sets containing const and/or volatile lvalue and
rvalue references (here's a link to those tests for the curious
http://home.twcny.rr.com/hinnant/cpp...lue_ref_test/).

Anyway, I've decided to make the fundamental combination (and
permutation) algorithms public. They can be found here, including a
couple of demoes:

http://home.twcny.rr.com/hinnant/cpp...binations.html

The second demo (involving clusters of cities) is perhaps the most
interesting. It demonstrates the ability to perform a combination
calculation that itself involves computing over combinations.
Admittedly the demo could be coded more efficiently. But hasn't been
for the purpose of demonstrating this nested combination computation
capability.

This set of algorithms is not necessarily a complete set. But I do not
have the time to extend it for now. I currently have:

template<class BidirectionalIterator, class Function, class Size>
Function
for_each_permutation(BidirectionalIterator first,
BidirectionalIterator last,
Size k, Function f);
template<class BidirectionalIterator, class Function, class Size>
Function
for_each_circular_permutation(BidirectionalIterato r first,
BidirectionalIterator last,
Size k, Function f);

template<class BidirectionalIterator, class Function, class Size>
Function
for_each_combination(BidirectionalIterator first,
BidirectionalIterator last,
Size k, Function f);

I wouldn't mind having "reversible permutation" versions. I.e. when you
consider a permutation and its reverse to be the same set, there's no
need to compute characteristics of both (e.g. if you're computing the
sum of distances between a sequence of locations, the distance is the
same whether the locations are traversed forwards or backwards). This
could cut the expense of the above permutation functions in half, but I
have not succeeded in coding those algorithms at this point.

Hth.

-Howard
Jan 2 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
3
by: Jonathan | last post by:
Hi all! For a match schedule I would like to find all possible combinations of teams playing home and away (without teams playing to themselves of course). I now the simple version works...
4
by: William D. Bartholomew | last post by:
We need to store land title information about properties in various Australian states, but each state maintains it's own land title registry and use different columns (well actually different...
19
by: Thomas Cameron | last post by:
Hello all, I have an array of values which I would like to generate all possible combinations for. And example would be the values "a,b,c". This would produce the following: a ab abc ac
3
by: Ryan | last post by:
I've been trying to find an algorithm that will output all of the possible combinations of items in an array. I know how to find the number of combinations for each set using nCr=n!/(r!(n-r)!) ...
3
by: Rahul Gupta | last post by:
I want to have certian key combinations which are hidden from user but if required i can use them to add some additional functionality to the program. I want to do this in C# 2.0 and i am using...
1
by: brendan | last post by:
Hi, I'm not at all competent in ms-sql, nor vb, as we work in Oracle and Mysql .... however, we need to port a couple of db queries to ms-access (2000) and I'm having a heck of a time trying to...
5
by: Bails | last post by:
Hi all I have a theory for a lotto system and need help on how to code it. I want to create 1 massive database with EVERY combination of numbers possible in a given lotto system, then remove all...
10
by: henryrhenryr | last post by:
Hi here's a test of logic for mathmeticians (I do have an actual use for this too...) I need to split a 4 or 5 word phrase into all possible combinations of the individual words: eg: 4...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.