User Tools

Site Tools


in204:tds:sujets:td3:part2

This is an old revision of the document!


Partie II – Fonction spécialisée

TD3

Question n°1

Nous considérons la fonction suivante que nous ajoutons à l’espace de nom generic. Expliquez ce qu’elle fait et tester là.

#include <stdio.h>
#include <random>
 
....
 
template<>
void populate_with_randoms(std::vector<std::string>& theVector,
    int theNumberOfValues, int theMinValue, int theMaxValue)
{
    // Initialise le générateur de nombres aléatoires
    // et définit la loi de distribution uniforme.
    std::random_device rd; 
    std::mt19937 generator(rd())
    std::uniform_int_distribution<int> 
        distribution(theMinValue, theMaxValue);
 
    char buffer[20];
 
    int width = theMaxValue - theMinValue;
    for(; 
        theNumberOfValues > 0; 
        theNumberOfValues--)
    {
 
        int randValue = distribution(generator);
        std::sprintf(buffer,"%d",randValue);
        std::string stringValue = buffer;
	theVector.push_back(stringValue);
    }
}

Correction

Correction

Supposons d'abord que nous souhaitons appelé la fonction generic::populate_with_randoms en lui passant un tableau de type std::string avec le code suivant :

#include <iostream>
#include"simple_sort.hpp"
#include<string>
 
using namespace std;
 
int main()
{
    {
        using namespace generic;
        std::vector<std::string> stringValues;
 
        populate_with_randoms(stringValues, 10, 'A', 'Z');
        print_vector(stringValues);
        simple_sort(stringValues);
        print_vector(stringValues);
    }
    return 0;

La compilation de ce code génère une erreur à la ligne :

namespace generic
{
    template<typename T, typename genType = int>
    void populate_with_randoms(
        std::vector<T>& theVector,
      	int theNumberOfValues, genType theMinValue, genType theMaxValue)
    {
        ...
        theVector.push_back(
            (T)distribution(gen));
    }
}

Question n°2

Nous souhaitons pouvoir choisir la fonction de comparaison utilisée dans le simple_sort. Pour l’instant, la fonction de comparaison fait toujours appel à l’opérateur >.

Nous souhaitons utiliser une classe traits qui fournira l’opération de comparaison. Par défaut, cette opération de comparaison est >.

Question n°2.1

Ecrire la classe greater_traits en complétant ce qui manque dans la classe suivante:

template<typename T>
struct greater_traits
{
public:
    static bool is_greater(T aValue, T anotherValue) {...}
};

Question n°2.2

Modifier la fonction simple_sort pour qu’elle prenne un argument supplémentaire qui est la classe fournissant l’opération de comparaison.

Question n°2.3

Proposer une classe lower_traits qui inverse l’ordre de tri.

Question n°2.4

Tester votre code sur l’exemple suivant :

#include"generic_sort.hpp"
#include<iostream>
 
int main()
{
    std::vector<int> array;
    generic::populate_with_randoms(array, 10, 1, 10);
    generic::print_vector(array);
    std::cout << "\n";
    generic::simple_sort(array);
    generic::print_vector(array);
    std::cout << "\n";
    generic::simple_sort<int, generic::lower_traits<int>>(array);
    generic::print_vector(array);
    return 0;
}
in204/tds/sujets/td3/part2.1570281919.txt.gz · Last modified: 2019/10/05 13:25 by bmonsuez