create table

Create an index table (indices are controlled). The idea is to make static tables
encapsulated in a function. This is to create a function t () returning a reference to integer int, and receiving an index in parameter.
The created function is used to answer the following main program:


void main()
{ t(3) = 5;
if (t(6) == 8)
t(6) = t(4);
t(1000) = 3; // Affichage d'une Erreur
t(­1) = 2; } // Affichage d'une Erreur

Who Can I help me please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <map>

int& t( int index )
{
    static std::map<int,int> table ;
    
    return table[index] ;
}

int main()
{
    t(3) = 5 ;
    t(4) = t(3) * 4 ;
    std::cout << t(4) << '\n' ; // 20

    if( t(6) == 8 ) t(6) = t(4) ;
    else t(6) = t(3) ;
    std::cout << t(6) << '\n' ; // 5

    t(1000) = 3;
    t(-1) = 2 ;
    // etc.
}
Topic archived. No new replies allowed.