Does anyone know how to list all variations of vector elements in C++ ?
Is there a function like for permutations (next_permutation) ?
tnx
Does anyone know how to list all variations of vector elements in C++ ?
Is there a function like for permutations (next_permutation) ?
tnx
Jump to PostAh ok I know what you want now but I'm going home in about 2 minutes...
Basically you need a recursive for loop (it may be wise to put it in its own method). You need an outer loop that iterates n times followed by inner loops that iterate vec.size() …
Jump to PostWhat you call variations are normally called combinations with repetition. Here is a sample program for you that does a subset of what you want. It is not be hard to extend the code to use an arbitrary list of values from a file, but Ed does not want to …
Tnx, I'm familiar with the next_permutation function.
Here's my problem, I have to write all variations of elements in the array
something like n=[1 2 3 ]
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
...
I can't use loop's because the number of elements will vary
combinations permutations, variations.
Which one will it be?
Each one is different.
So basically you just need to print out the entire contents of the array?
Of course you can use loops with varying length arrays. Use iterators.
@Ketsuekiame
How?
example, my array is n=[1 2 3], and my variations will have 2 elements (this number will vary, but the array is always the same)
Output should be
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Ok, so you have a 2 dimensional array that will always have the same length, but the contents won't always meet the length of the array?
Sorry, I'll start over.
The elements in my array are 1,2,3.(the array has 3 elements)
The input (example 2) means how many elements are in a single variation.
Example array=1 2 3 n=2
My output should be
1 1 (2 elements)
1 2 (2 elements)
2 1 (2 elements)
2 2 (2 elements)
2 3 (2 elements)
3 1 (2 elements)
3 2 (2 elements)
3 3 (2 elements)
For n=1 my output should be
1 (1 element)
2 (1 element)
3 (1 element)
For n=4 my output should be
1 1 1 1 (4 elements)
1 1 1 2 (4 elements)
.
.
.
3 3 3 1
3 3 3 2
3 3 3 3
You're mixing so many terms you've lost me. Sorry, but I don't understand what you want.
e.g. Yes, my 2 dimensional array will alway have the same length (example {1,2,3})
Isn't that a 3D array? (Although with those brackets, that's a 1D array with 3 elements...)
Then you input a single number? Into where?
The last line I just don't understand at all. Sorry.
Edit: It may help if you post code on your array declaration and how you fill it. ^^
Here's my code:
vector<string> vec;
string letters;
ifstream input ("input.txt");
getline(input,letters); //in the txt file input.txt is a string of characters (example ABCD)
n=letters.length();
vec.push_back("q1");
vec.push_back("q2");
vec.push_back("q3");
string array;
array="";
for(i=0;i<vec.size();i++)
{
array.append(vec);
}
Output for this array(q1,q2,q3) and n=4
q1 q1 q1 q1
q1 q1 q1 q2
...
...
q3 q3 q1 q1
q3 q3 q1 q2
q3 q3 q1 q3
q3 q3 q2 q1
q3 q3 q2 q2
q3 q3 q2 q3
q3 q3 q3 q1
q3 q3 q3 q2
q3 q3 q3 q3
In future, please wrap in code tags. But I see now.
Ok you have a vector of strings so vec[0] is "q1" and vec[1] is "q2"
Ok, that's fine. You then put all the elements into a single string, so you end up with "q1q2q3q4"
So based on the code you show above, what are you trying to find and from which variable are you trying to find it?
I don't have to find anything, I just have to list all variations. The number of elements in a single variation is the number of characters in a string from the txt file input.txt.
In this example my string is ABCD -> 4 characters, so every variation has to have 4 elements.
q1q1q1q1 (4 elements)
...
...
q3q3q3q1 (4 elements)
q3q3q3q2 (4 elements)
q3q3q3q3 (4 elements)
In this case the number of variations is 81. (3x3x3x3=81)
Ah ok I know what you want now but I'm going home in about 2 minutes...
Basically you need a recursive for loop (it may be wise to put it in its own method). You need an outer loop that iterates n times followed by inner loops that iterate vec.size() times.
It can be done but I don't have time to write it all out. I will be home at around 9pm British time. If someone else can do this, please help them =)
Thanks for the idea.
If you find the time these days, please post your code here.
What you call variations are normally called combinations with repetition. Here is a sample program for you that does a subset of what you want. It is not be hard to extend the code to use an arbitrary list of values from a file, but Ed does not want to give away the whole thing. :)
#include <algorithm>
#include <iostream>
#include <vector>
namespace Ed {
typedef std::vector<int> IntVec;
bool next_combination(IntVec& mset, int first, int last)
{
IntVec::size_type i = mset.size() - 1;
for (++mset[i]; i > 0 && mset[i] >= last; ++mset[i])
mset[i--] = first;
return mset.front() != last;
}
}
int main()
{
using namespace std;
using namespace Ed;
IntVec mset(2, 1);
do {
copy(mset.begin(), mset.end(), ostream_iterator<int>(cout, " "));
cout << '\n';
} while (next_combination(mset, 1, 4));
}
Thanks!
We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.