|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
|
[C++]Consiglio
E meglio
Codice:
/**
<b>Name</b>:Find\n
<b>Return</b>:A Value of option\n
<b>Argoments</b>: The name of option \n
<b>Description</b>: Findo a option\n
*/
string CParse::Find(string key)
{
string ret;
vector<Info>::iterator It = conf.begin();
for(; It != conf.end();++It)
{
if(It->key == key)
ret = It->value;
}
return ret;
}
Codice:
/**
<b>Name</b>:Find\n
<b>Return</b>:A Value of option\n
<b>Argoments</b>: The name of option \n
<b>Description</b>: Findo a option\n
*/
void CParse::Find(string * key)
{
vector<Info>::iterator It = conf.begin();
for(; It != conf.end();++It)
{
if(It->key == key)
key = It->value;
}
}
Tnk
__________________
Gnu/Linux User
|
|
|
|
|
|
#2 |
|
Bannato
Iscritto dal: Jul 2000
Città: Malo (VI)
Messaggi: 1000
|
Secondo me e' meglio restituire il valore della ricerca, visto che logicamente il valore di una opzione e' ben diverso dal valore associato.
In ogni caso una delle due versioni e' sbagliata, in una consideri Info.value una stringa, nell'altro un puntatore a stringhe... quale e' sbagliata. E se la stringa cercata non c'e' che succede ? Infine visto che usi contenitori della libreria, non riscrivere quello che c'e' gia', usa il metodo find e sei a posto. Ad esempio (modulo errori ovviamente Codice:
/**
<b>Name</b>:Find\n
<b>Return</b>:A Value of option\n
<b>Argoments</b>: The name of option \n
<b>Description</b>: Findo a option\n
*/
const string& CParse::Find(string key)
{
vector<Info>::iterator It = conf.find(key);
if ( It == conf.end() )
{
// non c'e'... possiamo gettare un'eccezione
// oppure, se decidiamo di tornare puntatori
// invece di stringhe, possiamo tornare un
// puntatore nullo
}
return It->value;
}
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:07.



















