Generate Permutations for a Given Set of Numbers

Generate Permutations for a Given Set of Numbers

There may come a time when you need to generate the permutations of a given set of variables at hand, all of them belonging to the same type.

The C++ STL library?algorithm provides a rich set of functions that you can use without reinventing the wheel. And one of them is the next_permutation function.

Here is some sample code that uses next_permutation:

#include <algorithm>#include #include #include using namespace std;int main(void) { 	vector v;	v.push_back(1);	v.push_back(2);	v.push_back(3);  // Initialize all elements of a vector	while (next_permutation(v.begin(), v.end() ) ) {                     // Loop until all permutations are generated.		copy(v.begin(), v.end(), ostream_iterator(cout, ""));		cout << endl;	}	return 0;}                     --   Code To Generate Permutations of a 3 digit number '123' --

The above code generates the permutation for a 3 digit number 123. It generates all possible permutations of the given vector, except one, the one that happens to be the original pattern itself. The output of the above code, generating permutations for a set of numbers 1, 2, and 3 is given as follows:

Output: 132213231312321

This code has been compiled on the major development platforms, namely GCC 3.2.3 on Linux, GCC 3.2 on Solaris, and MS VC++ 6.0?it was found to be working fine.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes