Hello, I have been interested in something kind of like the
next_permutatio n from the STL algorithm library, except that I want it
to find possible combinations of vector elements. Here is a more
detailed example of what I want:
Given a vector containing an arbitrary number of vectors, each of which
contains an arbitrary number of elements, generate a new vector in
which each element consists of one element taken from its corresponding
inner vector. Generate all possible combinations of this new vector.
I'm sure this is confusing, and my wording is not very good, so here is
a concrete example of what I'm looking for:
original vector a = [[1, 2, 3], [4, 5, 6], [7, 8]]
first combination would be [1, 4, 7]
next combination would be [1, 4, 8]
next combination would be [1, 5, 7]
next combination would be [1, 5, 8]
next combination would be [1, 6, 7]
next combination would be [1, 6, 8]
next combination would be [2, 4, 7]
next combination would be [2, 4, 8]
next combination would be [2, 5, 7]
next combination would be [2, 5, 8]
next combination would be [2, 6, 7]
next combination would be [2, 6, 8]
next combination would be [3, 4, 7]
next combination would be [3, 4, 8]
next combination would be [3, 5, 7]
next combination would be [3, 5, 8]
next combination would be [3, 6, 7]
last combination would be [3, 6, 8]
If you know before coding what the size of the original vector is going
to be, this can be accomplished with a simple set of nested loops.
However, making it work with arbitrary sizes of vectors is harder. It
would be even cooler if I could come up with some code that could work
with any level of vector nesting, producing something like the boost
library's multi-dimensional arrays for vectors with more than two
levels of nesting.
Any thoughts? I'm not an expert at C++ templates so this is somewhat
difficult for me.
Thanks,
Carl Youngblood 8 10953
"cayblood" <ca************ *@gmail.com> wrote in message
news:11******** ************@z1 4g2000cwz.googl egroups.com... Hello, I have been interested in something kind of like the next_permutatio n from the STL algorithm library, except that I want it to find possible combinations of vector elements. Here is a more detailed example of what I want:
Given a vector containing an arbitrary number of vectors, each of which contains an arbitrary number of elements, generate a new vector in which each element consists of one element taken from its corresponding inner vector. Generate all possible combinations of this new vector.
I'm sure this is confusing, and my wording is not very good, so here is a concrete example of what I'm looking for:
original vector a = [[1, 2, 3], [4, 5, 6], [7, 8]]
first combination would be [1, 4, 7] next combination would be [1, 4, 8] next combination would be [1, 5, 7] next combination would be [1, 5, 8] next combination would be [1, 6, 7] next combination would be [1, 6, 8] next combination would be [2, 4, 7] next combination would be [2, 4, 8] next combination would be [2, 5, 7] next combination would be [2, 5, 8] next combination would be [2, 6, 7] next combination would be [2, 6, 8] next combination would be [3, 4, 7] next combination would be [3, 4, 8] next combination would be [3, 5, 7] next combination would be [3, 5, 8] next combination would be [3, 6, 7] last combination would be [3, 6, 8]
If you know before coding what the size of the original vector is going to be, this can be accomplished with a simple set of nested loops. However, making it work with arbitrary sizes of vectors is harder. It would be even cooler if I could come up with some code that could work with any level of vector nesting, producing something like the boost library's multi-dimensional arrays for vectors with more than two levels of nesting.
Any thoughts? I'm not an expert at C++ templates so this is somewhat difficult for me.
I don't see a real problem here. To iterate through all the elements of a
vector is simple, and you don't have to know the vector size. If you were
working with arrays it would be difficult since you then have to know, but
with vectors you don't.
std::vector<std ::vector<int>>: :interator OuterIt;
std::vector<int >::iterator InnerIt;
for ( OuterIt = MyVector.begin( ); OuterIt != MyVector.end(); ++OuterIt)
for ( InnerIt = (*OuterIt).begi n(); InnerIt != (*OuterIt).end( );
++InnerIt )
// permiations
Check syntax on the (*OuterIt).begi n() etc... it might be a little off.
cayblood wrote: Hello, I have been interested in something kind of like the next_permutatio n from the STL algorithm library, except that I want it to find possible combinations of vector elements. Here is a more detailed example of what I want:
Given a vector containing an arbitrary number of vectors, each of which contains an arbitrary number of elements, generate a new vector in which each element consists of one element taken from its corresponding inner vector. Generate all possible combinations of this new vector.
I'm sure this is confusing, and my wording is not very good, so here is a concrete example of what I'm looking for:
original vector a = [[1, 2, 3], [4, 5, 6], [7, 8]]
first combination would be [1, 4, 7] next combination would be [...]
If you know before coding what the size of the original vector is going to be, this can be accomplished with a simple set of nested loops. However, making it work with arbitrary sizes of vectors is harder. It would be even cooler if I could come up with some code that could work with any level of vector nesting, producing something like the boost library's multi-dimensional arrays for vectors with more than two levels of nesting.
Any thoughts? I'm not an expert at C++ templates so this is somewhat difficult for me.
There is nothing essentially 'C++' and nothing 'template' here. Given that
you have a list of lists, initialise a list of indices to "all zeros", that
will be the helper for your first combination. From the current combination
(from the helper, actually), figure out the next combination. A way to do
that is a loop from the last index towards the first, add 1, see if you
slipped beyond the corresponing list's size. If so, reset to 0 and do the
previous. Repeat until you reach all 0 again or the combination is the
last one (whatever suits you).
helper = 0; // reset all helper elements
while (combination_is _valid(helper)) {
print_out_combi nation(helper);
to_increment = last;
while (to_increment > 0 && ++helper[to_increment] ==
list[to_increment].size())
helper[to_increment--] = 0;
}
V
But do you have any ideas on how to make it work for arbitrarily-deep
levels of nesting?
One other thing--I wanted to write a template for this so that it would
be as generic as the STL permutation routines and would work on any
applicable container rather than just vectors.
cayblood wrote: One other thing--I wanted to write a template for this so that it would be as generic as the STL permutation routines and would work on any applicable container rather than just vectors.
Write it twice, once for a set of lists and for a list of vectors.
That should give you an excellent idea how to generalise the algo.
V
>Write it twice, once for a set of lists and for a list of vectors. That should give you an excellent idea how to generalise the algo. V
Thanks for the advice. Will do.
On 2005-11-01, cayblood <ca************ *@gmail.com> wrote: One other thing--I wanted to write a template for this so that it would be as generic as the STL permutation routines and would work on any applicable container rather than just vectors.
See if you can find a copy of _Accelerated C++_ in a library or
bookstore. It contains what you need.
In short, write your algorithm to use iterators. Make sure to use
the most restrictive form of iterator you can. You should be able
to accomplish this algorithm with forward iterators (they support
only ++ and ==), which means it will be compatible with most
sequences.
--
Neil Cerutti
cayblood wrote: But do you have any ideas on how to make it work for arbitrarily-deep levels of nesting?
Recursion, I believe, should help.
V This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jose luis fernandez diaz |
last post by:
Hi,
I want to write a function of four parameters. Each parameter can be
of type long, double or string. For example:
f(long, long, long, long);
f(long, long, long, double);
f(long, long, long, const string &);
f(long, long, double, long);
f(long, long, string, long);
|
by: Peter R |
last post by:
Hi,
Probably this question was asked before, but what is the algorithm to
generate all combinations based on N numbers?
For example, for 3 elements = {1,2,3}, I'd like to generate a list of
(2^n - NULL):
1, 2, 3
1, 2
2, 3
|
by: Pelle Beckman |
last post by:
Hello,
This is OT, but since everybody here seems to have
a great knowledge of C++ I thought I'd ask.
I have this vector with n^2 elements (where n usually
is somewhere between 100 and 1000...)
My goal is to loop through these values (this is not
a real-time computation...) 5000 times or so, select a
random element from the vector and do some simple
|
by: spakka |
last post by:
I'm useless at templates, but, inspired by this post:
<hinnant-074D8A.11281207032005@syrcnyrdrs-01-ge0.nyroc.rr.com>,
I'm trying to write an algorithm
template <class BidirectionalIterator, class Function, class Size>
Function for_each_combination(BidirectionalIterator first,
BidirectionalIterator last,
Size k,
|
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)!)
set being possible combinations of r objects from a group of n objects.
e.g. n=5 r=3 using A,B,C,D,E as items
ABC ABD ABE ACD ACE
| |
by: Draw |
last post by:
Hi All,
Just a thought, about the find() algorithm in the C++ STL. I read that
the find algorithm can take a range of iterators. If it does not find
the element it is looking for in that range it returns the iterator to
the last element in the range, not to the last element in the
container, to the last element in the range. That being said, how can
we tell if find() has been successful in finding the element we need?
Its easy when we...
|
by: Sherrie Laraurens |
last post by:
Hi all,
I'm trying to write a generic algorithm routine that will
take begin and end iterators of a container, iterate through
the range and perform a "calculation" of sorts.
The trouble is that the algorithm will behave differently for
the two different types. I've considered calling the algorithm
foo_A and foo_B, but I don't like that approach because it will
blow out in naming complexity down the track.
|
by: Gary Wessle |
last post by:
Hi
I came up with this code which prints out all combinations as in
"c_choose_k"
so, if we have a vector of char abcdef, and we want all combinations
with 3 elements only, we make the 3 for loops as below,
int main(){
vector<strings;
s.push_back("1");
|
by: arnuld |
last post by:
WANTED:
/* C++ Primer - 4/e
*
* Exercise: 9.26
* STATEMENT
* Using the following definition of ia, copy ia into a vector and
into a list. Use the single iterator form of erase to remove the
elements with odd values from your list * and the even values from your
vector.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |