How to generate combination of vectors [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
combination and permutation in C++
I have a vector of say size "n". Lets say for example a vector of n=4, <1,2,3,4>. How can I generate all n-1 combinations of this vector. In this example, 4 chose 3. I want the output to be <1,2,3> <1,2,4> <1,3,4> <2,3,4>. Thanks.

Start by looking up STL's next_permutation function.

Related

Is there a built-in function that will shuffle an array? [duplicate]

This question already has answers here:
Is it possible to random_shuffle an array of int elements?
(5 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I am trying to create a deck of cards in C++. I store that deck in an array array<Card, 52> deckOfCards = generateCards(). Once I create the array, is there any way to shuffle the objects inside of it easily? For example, in Java, you could use the shuffle() algorithm and the objects would be shuffled.
People have mentioned the usage of random_shuffle in the comment, however random_shuffle has been deprecated since C++14, and removed since C++17.
Yes, C++ does have a shuffle() function. However, you must also add a random generator in there manually, which could be daunting at a first glance. However, if you don't really care the exact generator and engine being used, then this could be done simply with:
std::shuffle(deckOfCards.begin(), deckOfCards.end(), std::default_random_engine{/*optional seed*/});
or
std::shuffle(deckOfCards.begin(), deckOfCards.end(), std::random_device{});
Where the first one is deterministic and the second one is non-deterministic.

Two dimensional array in c++ initialization to one value [duplicate]

This question already has answers here:
how to set or initialize default value for all elements of a table or 2d array or multdimentional array
(4 answers)
Closed 3 years ago.
I have two dimensional char array in c++ and want to give all element one value without using loops
is there any function to achieve it ?
char demo[5][7];
memset(demo, '\0', sizeof(demo));
Based on the provided info, does this do what you want?
Since you're using C++ I'd question to use a native char array, as long as you're not required to and/or needing to use the stack.
Otherwise its idiomatic to use the collection classes provided by the STL.
Adding to the answer as requested, this obviously does what the question asks for: initializing the memory of a char array.
Please see the docs for more information on memset: https://en.cppreference.com/w/cpp/string/byte/memset

How to input unknown number of integers as uesr input in C++ [duplicate]

This question already has answers here:
How can I split a string by a delimiter into an array?
(13 answers)
Closed 3 years ago.
I want to get an unknown number of integers separated by space as user input. How can I store them in an array?
You could keep changing the size of the array (since you don't know how big to make it). Or use a data structure (like a vector) designed to change size.

Sort a Map<string,int> in C++ in descending order of Value [duplicate]

This question already has answers here:
Sorting a std::map by value before output & destroy
(5 answers)
Closed 8 years ago.
I have a need to sort a Map(Key=Word,Value=CountofWord) in the descending order of the count of occurrences and print top 10 words in the Map. What can be the best possible method to do that?
My idea is to build a Multimap(Key=CountofWord,Values=Words) and then print the top 10 elements of the Multimap. But this would take O(n) extra space.
Can there be any other optimized solution?
Just to point out that your proposed solution does not take into account that two counts of word can have the same value, therefore two words would be mapped to the same key and override the last one.
What you could do instead is make a class of a word with member variables the word as string and the count as int, and implement the < operator to allow sorting of the objects by the std::sort method. Refer to http://www.cplusplus.com/reference/algorithm/sort/.

can we have variable size array at arduino? [duplicate]

This question already has answers here:
Vectors in Arduino
(7 answers)
Closed 9 years ago.
I have the following variables:
char* words[20];
char* tracks[20];
where they represent the number of tracks and words saved in a database on a SD card. My question is that, I would like the array of words and tracks to be of variable size and not static, that it is I don't want to specify the size of the array.
I suggest you take a look at Standard C++ for Arduino It lets you use STL (standard template library) vector