Puis-je jouer aussi?
Ce qui suit est ma solution, basée essentiellement sur la spécialisation partielle de modèle.
#include <iostream>
using IType = int; // or long? or unsigned? or size_t?
template <typename IT, IT ...>
struct intSeq
{ };
template <typename, typename, std::size_t>
struct no_more_than_val;
template <typename IT, IT I0, std::size_t M, IT Val, IT ... Is>
struct no_more_than_val<intSeq<IT, I0, Is...>, intSeq<IT, Val>, M>
{ static constexpr bool value
{ no_more_than_val<intSeq<IT, Is...>, intSeq<IT, Val>, M>::value }; };
template <typename IT, IT Val, std::size_t M, IT ... Is>
struct no_more_than_val<intSeq<IT, Val, Is...>, intSeq<IT, Val>, M>
{ static constexpr bool value
{ no_more_than_val<intSeq<IT, Is...>, intSeq<IT, Val>, M-1U>::value }; };
template <typename IT, IT I0, IT ... Is>
struct no_more_than_val<intSeq<IT, I0, Is...>, intSeq<IT, I0>, 0U>
{ static constexpr bool value { false }; };
template <typename IT, std::size_t M, IT Val>
struct no_more_than_val<intSeq<IT>, intSeq<IT, Val>, M>
{ static constexpr bool value { true }; };
template <typename, std::size_t>
struct no_more_than_list;
template <typename IT, std::size_t M>
struct no_more_than_list<intSeq<IT>, M>
{ static constexpr bool value { true }; };
template <typename IT, IT I0, IT ... Is>
struct no_more_than_list<intSeq<IT, I0, Is...>, 0U>
{ static constexpr bool value { false }; };
template <typename IT, std::size_t M, IT I0, IT ... Is>
struct no_more_than_list<intSeq<IT, I0, Is...>, M>
{
static constexpr bool value
{ no_more_than_val<intSeq<IT, Is...>, intSeq<IT, I0>, M-1U>::value
&& no_more_than_list<intSeq<IT, Is...>, M>::value };
};
template <IType ... Is>
struct appearance
: public no_more_than_list<intSeq<IType, Is...>, 2U>
{ };
int main()
{
std::cout << appearance<0,1,0,1>::value << std::endl; // print 1
std::cout << appearance<2,0,1,2,2>::value << std::endl; // print 0
std::cout << appearance<5,5,5>::value << std::endl; // print 0
}
Si vous pouvez utiliser C++ 14, vous pouvez utiliser std::integer_sequence
au lieu de intSeq
. Quoi qu'il en soit, vous pouvez utiliser std::integral_constant<IT, Val>
au lieu de intSeq<IT, Val>
.
double possible de [Trouver nombre de valeurs uniques d'un pack de paramètres] (http://stackoverflow.com/questions/ 37150354/find-number-of-unique-values-of-a-parameter-pack) – gbjbaanb
Parce que dans '<0,1,0,1>' '0' et '1' n'apparaissent que deux fois alors que dans' <2,0,1,2,2> '' 2' apparaît trois fois. Je veux vérifier si un paramètre apparaît plus de ** deux fois **. –
Bien sûr, mais le principe est le même, cette réponse vous donne tout ce que vous devez savoir, sauf comment compter plus de 2. – gbjbaanb