how can I find repeated elements in a vector [duplicate] - c++

This question already has answers here:
Checking for duplicates in a vector [duplicate]
(5 answers)
Closed 9 years ago.
I have a vector of int which can include maximum 4 elements and minimum 2, for example :
std::vector<int> vectorDATA(X); // x means unknown here
What I want to do is to erase the elements that are repeated for example :
vectorDATA{1,2,2} to vectorDATA{1,2}
vectorDATA{1,2,3} to nothing changes
vectorDATA{2,2,2} to vectorDATA{2}
vectorDATA{3,2,1,3} to vectorDATA{3,2,1}
vectorDATA{1,2,1,2} to vector{1,2}
and so on
here a code simple :
cv::HoughLines(canny,lineQ,1,CV_PI/180,200);
std::cout << " line Size "<<lineQ.size()<< std::endl;
std::vector<int> linesData(lineQ.size());
std::vector<int> ::iterator it;
if(lineQ.size() <=4 && lineQ.size() !=0 ){
if(lineQ.size()==1){
break;
}else {
for ( int i = 0; i<lineQ.size();i++){
linesData[i] = lineQ[i][1]; // my comparison parameter is the lineQ[i][1]
}
// based on the answer I got I'm trying this but I really don't how to continue ?
std::sort(lineQ.begin(),lineQ.end(),[](const cv::Vec2f &a,const cv::Vec2f &b)
{
return ????
}
I tried use a for and do while loop, but I didn't get it, and the function std::adjacent_find this has a condition that the elements should be consecutive.
Maybe it's easy but I just don't get it !
thanks for any help !

The easy way is sort then unique-erase, but this changes order.
The c++11 order preserving way is to create an unordered_set<int> s; and do:
unordered_set<int> s;
vec.erase(
std::remove_if( vec.begin(),vec.end(), // remove from vector
[&](int x)->bool{
return !std::get<1>(s.insert(x)); // true iff the item was already in the set
}
),
vec.end() // erase from the end of kept elements to the end of the `vec`
);
which is the remove-erase idiom using the unordered_set to detect duplicates.

I didn't see a sort-less source code in the already mentioned answers, so here it goes. Hash table for checking duplicates, shifting unique elements towards the front of the vector, note that src is always >= dst and dst is the number of copied, i.e. unique elements at the end.
#include <unordered_set>
#include <vector>
#include <iostream>
void
uniq (std::vector<int> &a) {
std::unordered_set<int> s;
size_t dst = 0;
for (size_t src = 0; src < a.size(); ++src) {
if (s.count (a[src]) == 0) {
s.insert (a[src]);
a[dst++] = a[src];
}
}
a.resize (dst);
}
int
main () {
std::vector<int> a = { 3, 2, 1, 3, 2, 1, 2, 3, 4, 5 ,2, 3, 1, 1 };
uniq (a);
for (auto v : a)
std::cout<< v << " ";
std::cout << std::endl;
}

If you want to realy remove repeated elements, you may try something like this:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int data[] = {1,2,3,2,1};
vector<int> vectorDATA = (&data[0], &data[0] + 5);
sort(vectorDATA.begin(),vectorDATA.end());
for(int i = 0; i < vectorDATA.size()-1; ++i)
{
if(vectorDATA[i] == vectorDATA[i+1])
vectorDATA.erase(vectorDATA.begin()+i+1);
}
for(int i = 0; i < vectorDATA.size();++i)
{
cout << vectorDATA[i] << " ";
}
cout << endl;
return 0;
}
Lack of of this method is then elements lost his order.

Related

Finding Duplicates in an array using a Set in C++

I am currently practicing for coding interviews and am working on a function that takes in an array and the size of that array and prints out which numbers in it are duplicates. I have gotten this to work using the two for loop method but want an optimized solution using sets. Snippet of the code I have is below,
#include <iostream>
#include <set>
using namespace std;
void FindDuplicate(int integers[], int n){
set<int>setInt;
for(int i = 0; i < n; i++){
//if this num is not in the set then it is not a duplicate
if(setInt.find(integers[i]) != setInt.end()){
setInt.insert({integers[i]});
}
else
cout << integers[i] << " is a duplicate";
}
}
int main() {
int integers [] = {1,2,2,3,3};
int n = sizeof(integers)/sizeof(integers[0]);
FindDuplicate(integers, n);
}
Any helpful advice is appreciated, thanks
I think your comparison is not needed, insert do it for you:
https://en.cppreference.com/w/cpp/container/set/insert
Returns a pair consisting of an iterator to the inserted element (or
to the element that prevented the insertion) and a bool value set to
true if the insertion took place.
Just insert element and check what insert function returns (false on second element of pair in case of duplication) :)
my solution proposal is :
count the frequencies of each element (algo for frequencies are explained here frequency
display elements with frequency more than 1 (it is a duplicate)
In each operation, you do not use imbricated loops.
#include <iostream>
#include <unordered_map>
using namespace std;
void FindDuplicate(int integers[], int n)
{
unordered_map<int, int> mp;
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++)
{
mp[integers[i]]++;
}
cout << "The repeating elements are : " << endl;
for (int i = 0; i < n; i++) {
if (mp[integers[i]] > 1)
{
cout << integers[i] << endl;
mp[integers[i]] = -1;
}
}
}
int main()
{
int integers [] = {1,1,0,0,2,2,3,3,3,6,7,7,8};
int n = sizeof(integers)/sizeof(integers[0]);
FindDuplicate(integers, n);
}
This is my feedback:
#include <iostream>
#include <vector>
#include <set>
// dont' do this, in big projects it's not done (nameclash issues)
// using namespace std;
// pass vector by const reference you're not supposed to change the input
// the reference will prevent data from being copied.
// naming is important, do you want to find one duplicate or more...
// renamed to FindDuplicates because you want them all
void FindDuplicates(const std::vector<int>& input)
{
std::set<int> my_set;
// don't use index based for loops if you don't have to
// range based for loops are more safe
// const auto is more refactorable then const int
for (const auto value : input)
{
//if (!my_set.contains(value)) C++ 20 syntax
if (my_set.find(value) == my_set.end())
{
my_set.insert(value);
}
else
{
std::cout << "Array has a duplicate value : " << value << "\n";
}
}
}
int main()
{
// int integers[] = { 1,2,2,3,3 }; avoid "C" style arrays they're a **** to pass around safely
// int n = sizeof(integers) / sizeof(integers[0]); std::vector (or std::array) have size() methods
std::vector input{ 1,2,2,3,3 };
FindDuplicates(input);
}
You do not need to use a set.
To find the duplicates:
Sort array with numbers
Iterate over the array (start with second element) and copy elements where previous element equals
current element into a new vector "duplicates"
(Optional) use unique on the "duplicates" if you like to know which number is a duplicate and do not care if it is 2, 3 or 4 times in the numbers array
Example Implementation:
#include <algorithm>
#include <iostream>
#include <vector>
void
printVector (std::vector<int> const &numbers)
{
for (auto const &number : numbers)
{
std::cout << number << ' ';
}
std::cout << std::endl;
}
int
main ()
{
auto numbers = std::vector<int>{ 1, 2, 2, 42, 42, 42, 3, 3, 42, 42, 1, 2, 3, 4, 5, 6, 7, 7 };
std::sort (numbers.begin (), numbers.end ());
auto duplicates = std::vector<int>{};
std::for_each (numbers.begin () + 1, numbers.end (), [prevElement = numbers.begin (), &duplicates] (int currentElement) mutable {
if (currentElement == *prevElement)
{
duplicates.push_back (currentElement);
}
prevElement++;
});
duplicates.erase (std::unique (duplicates.begin (), duplicates.end ()), duplicates.end ());
printVector (duplicates);
}
edit:
If you have no problem with using more memory and more calculations but like it more expressive:
Sort numbers
Create a new array with unique numbers "uniqueNumbers"
Use "set_difference" to calculate (numbers-uniqueNumbers) which leads to an new array with all the duplicates
(Optional) use unique on the "duplicates" if you like to know which number is a duplicate and do not care if it is 2, 3 or 4 times in the numbers array
Example Implementation:
#include <algorithm>
#include <iostream>
#include <vector>
void
printVector (std::vector<int> const &numbers)
{
for (auto const &number : numbers)
{
std::cout << number << ' ';
}
std::cout << std::endl;
}
int
main ()
{
auto numbers = std::vector<int>{ 2, 2, 42, 42, 42, 3, 3, 42, 42, 1, 2, 3, 4, 5, 6, 7, 7 };
std::sort (numbers.begin (), numbers.end ());
auto uniqueNumbers = std::vector<int>{};
std::unique_copy (numbers.begin (), numbers.end (), std::back_inserter (uniqueNumbers));
auto duplicates = std::vector<int>{};
std::set_difference (numbers.begin (), numbers.end (), uniqueNumbers.begin (), uniqueNumbers.end (), std::back_inserter (duplicates));
std::cout << "duplicate elements: ";
printVector (duplicates);
std::cout << "unique duplicate elements: ";
printVector ({ duplicates.begin (), std::unique (duplicates.begin (), duplicates.end ()) });
}
here's a quick solution use an array of size N (try a big number)
and whenever a number is added into the other array on the large array add 1 to the position like:
array_of_repeated[user_input]++;
so if the program asks how many times (for example) number 234 was repeated?
std::cout<<array_of_repeated[requested_number]<<std::endl;
so in this way you wont spend time looking for a number inside the other list

How to remove all instances of a duplicate from a vector<int> [duplicate]

This question already has answers here:
How to remove duplicated items in a sorted vector
(7 answers)
Closed 2 years ago.
I've been trying to solve an "easy" leetcode question for two hours now where I need to remove every instance of an int that appears more than once then add the non dupes together. I've tried about 10 different ways and I can only get it to go down to one copy of each int. Here is the nicest solution I've written, but given input {1,2,2,3,4} it would return {1,2,3,4} whereas the expected output is {1,3,4}
sort(nums.begin(), nums.end()); //Sort numerically
nums.erase(unique(nums.begin(), nums.end()), nums.end());
return (accumulate(nums.begin(), nums.end(), 0));
NlogN complexity, you avoid needing to pre-sort:
Godbolt
#include <set>
#include <vector>
int main()
{
std::vector<int> nums = {1,2,2,3,5};
std::multiset<int> m (nums.begin(), nums.end());
std::size_t sum = 0;
for (const auto& elem : m)
{
sum += m.count(elem) == 1 ? elem : 0;
}
}
Update: Could use std::unordered_multiset, as we don't need ordering.
There are many workarounds, you can iterate over the array and see how many time the number exists using map or something, and then you iterate again over the map and add the number that only appeared once to a new array.
You can use a set and a map<bool,int>, every time you add a new number you check if it exists or not.
if(!map[number]){
set.insert(number);
map[number]=true;
}else{
set.erase(number);
}
You can use a std::unordered_map<int, bool> where the second (template) parameter (bool) specifies whether the value is duplicated or not. It'll look something like this,
#include <unordered_map>
...
std::unordered_map<int, bool> uniqueCheckMap;
for (auto i : nums)
{
if (uniqueCheckMap.find(i) == uniqueCheckMap.end())
uniqueCheckMap[i] = false;
else
uniqueCheckMap[i] = true; // Set the second (value) as true if duplicate entry is found.
}
nums.clear();
int sum = 0;
for (auto i : uniqueCheckMap)
{
if (!i.second)
{
nums.push_back(i.first);
sum += i.first;
}
}
If O(n²) is not a problem...
const auto sum=std::accumulate(cbegin(nums), cend(nums), 0,
[&](const auto &accum, const auto &elem)
{
return accum+(std::count(cbegin(nums), cend(nums), elem)>1 ? 0 : elem);
});
set_difference is a great help here. It removes occurences in the way you describe.
auto ints = vector{4, 3, 2, 2, 1};
sort(begin(ints), end(ints));
std::vector<int> unique_ints;
unique_copy(begin(ints), end(ints), back_inserter(unique_ints));
vector<int> doubles;
set_difference( begin(ints), end(ints),
begin(unique_ints), end(unique_ints),
back_inserter(doubles)
);
vector<int> only_once;
set_difference( begin(unique_ints), end(unique_ints),
begin(doubles), end(doubles),
back_inserter(only_once));
copy(begin(only_once), end(only_once), ostream_iterator<int>(cout, ", "));
cout << "\n";
Also... it seems to do the trick.
$ g++ -std=c++17 u.cpp && ./a.out
1, 3, 4,
Or you could use a loop over 'equal ranges', only accumulating the ones that are a single element long:
sort(begin(ints), end(ints));
int acc = 0;
auto range = equal_range(begin(ints), end(ints), ints.front());
while(range.first != end(ints)) {
cout << *range.first << "+ ";
if (distance(range.first, range.second) == 1){
acc += *range.first;
}
range = equal_range(range.second, end(ints), *range.second);
}
cout << " = " << acc << "\n";

bubble sort in c++ with an array of 10 random ints isnt working

Run code here
this is my code for my bubble sort in c++ that for some reason only bubble sorts the last few but not the first 5 ints in the array. Help please
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
/* Tom Dresner
* 3/17/17
*/
int tom[10];
int b;
int x;
void sort()
{
cout << "\n";
for(int b=0;b<10;b++)
{
for(int x=0;x<9;x++)
{
if(tom[x]>tom[x + 1])
{
int t = tom[x];
tom[x] = tom[x+1];
tom[x+1] = t;
}
}
cout<< tom[b] << endl;
}
}
int main()
{
cout << "\n";
for(int i=0;i<10;i++)
{
tom[i] = rand()%10;
cout<< tom[i] << endl;
}
sort();
}
Not a complete answer, because that looks like homework, but here is a solution to a related problem that shows what kind of techniques you can use. In particular, a way to generate and return data without using globals, and a way to print it.
You may want to sort in place instead of what I do below. If so, you will find std::swap(v[i],v[j]) from the header <algorithm> helpful. You would also want to take a std::vector<int>& reference as your argument and return the same reference, like I do for std::ostream& os.
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::size_t;
std::vector<int> merge( const std::vector<int>& a, const std::vector<int>& b )
/* Given two sorted vectors, a and b, returns a sorted vector that merges the
* elements of a and b. Maybe you can figure out how this would be useful
* for sorting faster than bubble sort? Or you can ask your professor.
*/
{
std::vector<int> output; // Will hold the results;
/* If you haven't seen iterators yet, they're like indices inside the array.
* The expression *it++ returns the value at position it, then increments
* it.
*/
std::vector<int>::const_iterator it = a.begin();
std::vector<int>::const_iterator jt = b.begin();
// Reserve as much memory as we will need, because resizing vectors is slow.
output.reserve( a.size() + b.size() );
while ( it < a.end() || jt < b.end() ) {
if ( it == a.end() ) // We've reached the end of a, but not b.
while ( jt < b.end() ) // So we can stop checking a.
output.push_back(*jt++); // Add each element to the end.
else if ( jt == b.end() ) // We've reached the end of b, but not a.
while ( it < a.end() )
output.push_back(*it++);
else if ( *jt >= *it ) // Always add the lowest remaining element.
output.push_back(*it++);
else // What is the only remaining case?
output.push_back(*jt++);
} // end while
/* Do we know for sure that we've added each element from a or b to output
* exactly once, and in the right order? We assume a and be were sorted
* already. On each invocation of the loops, we copied exactly one element
* from either a or b to output, and stepped past it. The element we added
* was the smallest remaining element of a, unless there were no more in a
* or the smallest remaining element of b was smaller. Otherwise, it was
* the smallest remaining element of b. We stopped when we ran out of
* elements in both a and b.
*/
output.shrink_to_fit(); // Maybe save a few bytes of memory.
// Check that we have the correct number of elements:
assert( output.size() == a.size() + b.size() );
return output;
}
bool is_sorted( const std::vector<int>& v )
// Returns true if and only if v is sorted.
{
// Check that the elements are sorted.
for ( size_t i = 1; i < v.size(); ++i )
if( v[i] < v[i-1] )
return false;
/* If we escape the loop, we tested each pair of consecutive elements of v,
* and none were out of order.
*/
return true;
}
std::ostream& operator<< ( std::ostream& os, const std::vector<int>& v )
// Boilerplate to serialize and print a vector to a stream.
{
const size_t size = v.size();
os << '[';
if (size > 0)
os << v[0];
for ( size_t i = 1; i < size; ++i )
os << ',' << v[i];
os << ']';
return os;
}
int main(void)
{
// Our sorted input lists:
const std::vector<int> a = {0, 2, 4, 6, 8};
const std::vector<int> b = {-3, -2, -1, 0, 1, 2, 3};
assert(is_sorted(a)); // Input validation.
assert(is_sorted(b));
//Our output:
const std::vector<int> sorted = merge(a, b);
assert(is_sorted(sorted)); // Output validation.
cout << sorted << endl;
return EXIT_SUCCESS;
}

How can I check if vector elements are in order consecutively?

I need to check if in my vector the elements are in order consecutively?
for(i=1; i<=K; i++)
if(v[i]=v[i+1]-1)
If the statement would be true I want to return the biggest integer.
ex. 4 5 6 7
7
There's an algorithm for that: std::is_sorted:
if (std::is_sorted(v.begin(), v.end()) {
return v.back(); // the largest element would be the last one
}
else {
// ??
}
I need to check if in my vector the elements are in order
Use C++11's std::is_sorted algorithm:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v1 { 4, 5, 7, 6 };
std::vector<int> v2 { 4, 5, 6, 7 };
using std::begin;
using std::end;
std::cout << std::is_sorted(begin(v1), end(v1)) << "\n";
std::cout << std::is_sorted(begin(v2), end(v2)) << "\n";
}
If the statement would be true I want to return the biggest integer.
That's a job for std::max_element:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v1 { 4, 5, 7, 6 };
std::vector<int> v2 { 4, 5, 6, 7 };
using std::begin;
using std::end;
std::cout << *std::max_element(begin(v1), end(v1)) << "\n";
std::cout << *std::max_element(begin(v2), end(v2)) << "\n";
}
Note that std::max_element does not require its input to be sorted.
Or if it's sorted anyway, just use v1.back().
I would use:
is_sorted(begin(v), end(v));
If the sequence is not sorted then use:
max_element(begin(v), end(v));
to get the max.
Use std::is_sorted algorithm of STL.
bool test(vector<int> v) {
for(int i = 0; i < v.size()-1; i++)
if(v[i] > v[i+1])
return false;
return true;
}
If the vector is in order, so the biggest is the last one, that is, v[v.size()-1].
From the question it seems you are interested if the vector elements are consective integers. If that is the case you can use a flag to indicate if the condition holds and do a single iteration:
bool consecutive = false;
for (int i = 1; i < v.size(); ++i) {
if (v[i] != v[i - 1] + 1) {
consecutive = false;
break;
}
}
if (consecutive) {
cout << v.back() << endl; // the last element is the biggest.
}
However your question title suggests you are interested in checking if the vector is sorted. If that is the case you can use the built-in function std::is_sorted that is present since c++11.
Traverse through the vector starting from the second element and keep a flag variable initialized to 0, for each iteration check the following condition :-
if(v[i]<v[i-1])
if this condition is true, then set the flag to 1 and break out of the loop. After the control moves out of the loop, check the value of flag. If flag is 1, then the vector elements are not in order. Otherwise, if the flag is 0, then the vector elements are in order and you have to print the largest element which is v[v.size()-1].

How to Create All Permutations of Variables from a Variable Number of STL Vectors [duplicate]

This question already has answers here:
Generate all combinations from multiple lists
(11 answers)
Closed 9 years ago.
I have a variable number of std::vectors<int>, let's say I have 3 vectors in this example:
std::vector<int> vect1 {1,2,3,4,5};
std::vector<int> vect2 {1,2,3,4,5};
std::vector<int> vect3 {1,2,3,4,5};
The values of the vectors are not important here. Also, the lengths of these vectors will be variable.
From these vectors, I want to create every permutation of vector values, so:
{1, 1, 1}
{1, 1, 2}
{1, 1, 3}
...
...
...
{3, 5, 5}
{4, 5, 5}
{5, 5, 5}
I will then insert each combination into a key-value pair map for further use with my application.
What is an efficient way to accomplish this? I would normally just use a for loop, and iterate across all parameters to create all combinations, but the number of vectors is variable.
Thank you.
Edit: I will include more specifics.
So, first off, I'm not really dealing with ints, but rather a custom object. ints are just for simplicity. The vectors themselves exist in a map like so std::map<std::string, std::vector<int> >.
My ultimate goal is to have an std::vector< std::map< std::string, int > >, which is essentially a collection of every possible combination of name-value pairs.
Many (perhaps most) problems of the form "I need to generate all permutations of X" can be solved by creative use of simple counting (and this is no exception).
Let's start with the simple example: 3 vectors of 5 elements apiece. For our answer we will view an index into these vectors as a 3-digit, base-5 number. Each digit of that number is an index into one of the vectors.
So, to generate all the combinations, we simply count from 0 to 53 (125). We convert each number into 3 base-5 digits, and use those digits as indices into the vectors to get a permutation. When we reach 125, we've enumerated all the permutations of those vectors.
Assuming the vectors are always of equal length, changing the length and/or number of vectors is just a matter of changing the number of digits and/or number base we use.
If the vectors are of unequal lengths, we simply produce a result in which not all of the digits are in the same base. For example, given three vectors of lengths 7, 4 and 10, we'd still count from 0 to 7x4x10 = 280. We'd generate the least significant digit as N%10. We'd generate the next least significant as (N/10)%4.
Presumably that's enough to make it fairly obvious how to extend the concept to an arbitrary number of vectors, each of arbitrary size.
0 - > 0,0,0
1 - > 0,0,1
2 - > 0,1,0
3 - > 0,1,1
4 - > 1,0,0
...
7 - > 1,1,1
8 - > 1,1,2
...
The map should translate a linear integer into a combination (ie: a1,a2,a3...an combination) that allows you to select one element from each vector to get the answer.
There is no need to copy any of the values from the initial vectors. You can use a mathematical formula to arrive at the right answer for each of the vectors. That formula will depend on some of the properties of your input vectors (how many are there? are they all the same length? how long are they? etc...)
Following may help: (https://ideone.com/1Xmc9b)
template <typename T>
bool increase(const std::vector<std::vector<T>>& v, std::vector<std::size_t>& it)
{
for (std::size_t i = 0, size = it.size(); i != size; ++i) {
const std::size_t index = size - 1 - i;
++it[index];
if (it[index] == v[index].size()) {
it[index] = 0;
} else {
return true;
}
}
return false;
}
template <typename T>
void do_job(const std::vector<std::vector<T>>& v, std::vector<std::size_t>& it)
{
// Print example.
for (std::size_t i = 0, size = v.size(); i != size; ++i) {
std::cout << v[i][it[i]] << " ";
}
std::cout << std::endl;
}
template <typename T>
void iterate(const std::vector<std::vector<T>>& v)
{
std::vector<std::size_t> it(v.size(), 0);
do {
do_job(v, it);
} while (increase(v, it));
}
This is an explicit implementation of what Lother and Jerry Coffin are describing, using the useful div function in a for loop to iterate through vectors of varying length.
#include <cstdlib> // ldiv
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
vector<int> vect1 {100,200};
vector<int> vect2 {10,20,30};
vector<int> vect3 {1,2,3,4};
typedef map<string,vector<int> > inputtype;
inputtype input;
vector< map<string,int> > output;
int main()
{
// init vectors
input["vect1"] = vect1;
input["vect2"] = vect2;
input["vect3"] = vect3;
long N = 1; // Total number of combinations
for( inputtype::iterator it = input.begin() ; it != input.end() ; ++it )
N *= it->second.size();
// Loop once for every combination to fill the output map.
for( long i=0 ; i<N ; ++i )
{
ldiv_t d = { i, 0 };
output.emplace_back();
for( inputtype::iterator it = input.begin() ; it != input.end() ; ++it )
{
d = ldiv( d.quot, input[it->first].size() );
output.back()[it->first] = input[it->first][d.rem];
}
}
// Sample output
cout << output[0]["vect1"] << endl; // 100
cout << output[0]["vect2"] << endl; // 10
cout << output[0]["vect3"] << endl; // 1
cout << output[N-1]["vect1"] << endl; // 200
cout << output[N-1]["vect2"] << endl; // 30
cout << output[N-1]["vect3"] << endl; // 4
return 0;
}
Use a vector array instead of separate variables. then use following recursive algorithm :-
permutations(i, k, vectors[], choices[]) {
if (i < k) {
for (int x = 0; x < vectors[i].size(); x++) {
choices[i] = x;
permutations(i + 1, k, vectors, choices);
}
} else {
printf("\n %d", vectors[choices[0]]);
for (int j = 1; j < k; j++) {
printf(",%d", vectors[choices[j]]);
}
}
}