Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Hash de hash de hash de hash en C++

1 réponse
Avatar
rp
Bonjour,
est-il possible d'=E9crire le code PERL suivant en C++ ?
Ce code cr=E9e un tableau de hachage qui contient des tableau de hachage
qui contiennent des tableaux de hachage ... qui contiennent une cl=E9
"count" qui est incr=E9ment=E9e selon qu'elle existe ou pas.
Merci de votre aide.
rp


-----------------------------------------
#!/opt/local/bin/perl
use strict;
use warnings;

my %hash =3D ();

for (my $y =3D 2000; $y < 2010; ++$y) {
for (my $m =3D 1; $m < 12; ++$m) {
for (my $d =3D 1; $d < 30; ++$d) {
process(\%hash, $y, $m, $d);
}
}
}
for (my $y =3D 2000; $y < 2010; ++$y) {
for (my $m =3D 1; $m < 12; ++$m) {
for (my $d =3D 1; $d < 30; ++$d) {
process(\%hash, $y, $m, $d);
}
}
}
use Data::Dump qw(dump);

print dump(%hash);


sub process {
my ($hash_ref,$y,$m,$d) =3D @_;


if ( exists ${$hash_ref}{$y}{$m}{$d}{count} ) {
${$hash_ref}{$y}{$m}{$d}{count}++;
} else {
${$hash_ref}{$y}{$m}{$d}{count} =3D 0;
}
}

1 réponse

Avatar
Pascal J. Bourguignon
rp writes:

est-il possible d'écrire le code PERL suivant en C++ ?



Oui, c'est possible.



// my %hash = ();

HashTable<int,HashTable<int,HashTable<int,HashTable<std::string,int> > > > hash;


// sub process {
// my ($hash_ref,$y,$m,$d) = @_;

void process(HashTable* hash,int y,int m,int d){
// if ( exists ${$hash_ref}{$y}{$m}{$d}{count} ) {

if((*hash)[y][m][d].exists("count")){

// ${$hash_ref}{$y}{$m}{$d}{count}++;

(*hash)[y][m][d]["count"]++;

// } else {

}else{

// ${$hash_ref}{$y}{$m}{$d}{count} = 0;

(*hash)[y][m][d]["count"]=0;

// }
// }

}
}


int main(void){

// for (my $y = 2000; $y < 2010; ++$y) {
// for (my $m = 1; $m < 12; ++$m) {
// for (my $d = 1; $d < 30; ++$d) {
// process(%hash, $y, $m, $d);
// }
// }
// }

for(int y 00;y<2010;++y){
for(int m=1;m<12;++m){ // on peut même dupliquer les bogues.
for(int d=1;d<30;++d){ // idem
process(&hash,y,m,d);
}
}
}

// for (my $y = 2000; $y < 2010; ++$y) {
// for (my $m = 1; $m < 12; ++$m) {
// for (my $d = 1; $d < 30; ++$d) {
// process(%hash, $y, $m, $d);
// }
// }
// }

for(int y 00;y<2010;++y){
for(int m=1;m<12;++m){ // on peut même dupliquer les bogues.
for(int d=1;d<30;++d){ // idem
process(&hash,y,m,d);
}
}
}


// use Data::Dump qw(dump);
//
// print dump(%hash);

std::cout<<hash.dump();
return(0);
}


Implémenter la template HashTable<KeyType,ValueType> est laissé comme
exercice pour le lecteur...


--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.