Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826

    [c++stl]mappe e transcodifica

    Ciao.
    Devo poter recuperare o settare il valore di un dato.
    E devo poterlo fare con l'indice intero o con l'indice stringa ma vorrei mantenere una sola mappa che contiene i valori.
    gli indici non sono consecutivi.
    ho pensato:
    codice:
    //indici
    mapIndexInt[0]="indice0";
    mapIndexInt[5]="indice1";
    
    //valori
    mapValue["indice0"]="aaaaaaa"
    mapValue["indice1"]="bbbbbb"
    mapValue["indice2"]="ccccccc"
    
    void setValue0ConInt(string value){
       string strIndice= mapIndexInt[0];//la funzione si chiama setValue0....= 0
       mapValue[strIndice] = value;
    };
    
    void setValue0ConText(string value){
    
     mapValue["indice0"] = value;//la funzione si chiama setValue0 ... = 0
    }
    
    string getValue0ConInt(){
    
       string strIndice= mapIndexInt[0];//v.sopra = 0
       return mapValue[strindice];
    
    }
    string getValue0ConText(){
       
       return mapValue["indice0"]//v sopra  =0 = indice0
    }
    E' sbagliato come metodo?
    voi come lo fareste?
    Grazie.

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Non puoi semplicemente convertire l'int in stringa?
    codice:
    string getValue0ConInt(){
    
       ostringstream oss;
       oss << "indice"<< 0;
       return mapValue[oss.str()];
    
    }
    string getValue0ConText(){
       
       return mapValue["indice0"]//v sopra  =0 = indice0
    
    }
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    no , scusa shodan se mi sono espresso male ,ho utilizzato indice0, indice1 ecc... per esempio , nel mio progetto hanno nomi generici non legati alla costruzione di indice con numeri.
    grazie.

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Nessun problema
    Se numeri e stringhe non sono relazionabili, la tua soluzione mi sembra l'unica possibile.
    L'unica cosa che valuterei è l'accesso a mapValue tramite int invece della stringa.
    Da l'idea di essere più veloce.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.