|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
[C++] funzione che ritorna vector<?>
Sto cominciando ad usare STL solo adesso e mi servirebbe (se possibile) creare una
funzione che ritorna un vector<tipo> dove tipo può essere (per adesso) un int o double si può e, se si, come si fa ? grazie
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Fai una funzione template
template <class T> vector<T> funzione(); |
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
EDIT: ho googlato questo: vector "unknown type" return function c++ ed è uscito l'impossibile grazie
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Certo
Dopo per richiamarla: vector<int> v = funzione<int>(); vector<double> v = funzione<double>(); |
|
|
|
|
|
#5 | |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
vabbè grazie di nuovo. Certo che è potente 'sto STL ... anche se in certe occasioni preferisco ancora usare funzioni C tipo strstr, strchr etc
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
|
|
|
|
|
|
#7 | |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
quello che non mi piace è che le varie find tornano sempre un indice e mai un char pointer quindi, trovato per esempio il valore, devi fare una substr illeggibile, A sto punto meglio un return strstr(...) è anche vero che certe volte ti semplificano la vita come per esempio in quest trim che ho fatto Codice:
string Util::trimString( const string& s )
{
unsigned int begin = s.find_first_not_of(' ');
unsigned int end = s.find_last_not_of (' ');
return s.substr( begin, end-begin+1);
}
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Appunto...basta lavorare esclusivamente con le string e sei a posto...poi se devi usare qualche libreria che ha come parametri char * basta convertire con c_str().
|
|
|
|
|
|
#9 | |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
È una funzione che cerca una stringa delimitata da certi caratteri in una stringa sorgente. Ho usato sia string che funzioni C All'inizio pSrc era di tipo string& poi ho sbroccato (non ricordo perchè ma per colpa di 'ste string) ed ho messo char* Codice:
string LLUtils::GetDelimitedString( const char* pSrc, int LeftDelimiter, int RightDelimiter )
{
LogL.Write( LogLib::LOGLIB_DEBUG_FLOW_MSG, LOGLIB_FIX_ARGS, 0, "" );
if (pSrc) // so the caller can avoid checking
{
char* start = strchr( pSrc, LeftDelimiter );
if (start) // left delimiter was found
{
while(*(++start) == ' ')
; // skip left spaces
char* end = strchr( start, RightDelimiter );
if (end) // right delimiter was found
{
while(*(--end) == ' ')
; // skip right spaces
string s(pSrc); // create the string to be returned
s.substr( (start - pSrc) );
return s.substr( (start - pSrc), (end - start + 1) );
}
}
}
return "";
}
////////////////////////////////////////////////////////////////////
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
|
#10 | |
|
Senior Member
Iscritto dal: Mar 2007
Messaggi: 1792
|
Quote:
|
|
|
|
|
|
|
#11 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Vediamo:
Codice:
string LLUtils::GetDelimitedString( const string &src, int LeftDelimiter, int RightDelimiter )
{
LogL.Write( LogLib::LOGLIB_DEBUG_FLOW_MSG, LOGLIB_FIX_ARGS, 0, "" );
string result = "";
size_t left = src.find_first_of(LeftDelimiter);
size_t right = src.find_last_of(RightDelimiter);
if(left != str::npos && right != str::npos)
{
result = Util::trimString(src.substr(left, right));
}
return result;
}
|
|
|
|
|
|
#12 | |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
Praticamente ci ho litigato per un pò, poi ho optato per ciò che conosco benissimo per non perdere tempo prezioso e invece era fattibilissimo. vabbè, grazie ancora
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
|
#13 |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
beh, visto che orami è una disputa (amichevole e costruttiva - per me almeno) tra STL e C functions, vorrei proporvi un'altra ... gara
(ed è quì che mi serviva una funzione che torna vector<type>) ho una stringa di questo formato "x < y" dove x è un valore minimo e y quello massimo. La funzione torna un vector di interi dal minimo al massimo. Quindi se il minimo è 1 e il massimo 5, torna 1 2 3 4 5 Calcolando che il tutto deve essere il più veloce possibile (sto lavorando ad un progetto per la comunicazione tra un carro armato e vari operatori) l'ho fatta così (io avrei fatto a meno anche del lento vector e fatto tutto con un array di int )Codice:
vector<int> LLUtils::GetMinToMaxValues( string& s, int Separator )
{
vector<int> v;
int Min = atoi( s.c_str() ); // 1st value, atoi stops at first non digit char
int Max = atoi( strchr(s.c_str(), Separator) + 1 ); // 2nd value, atoi from char after Separator
for ( int i = Min; i <= Max; ++i )
v.push_back(i); // creates the list
return v;
}
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
#14 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Certo che anche la tua non va male...ma quando posso tento di non mischiare C e C++.
Il codice sotto in teoria non necessita del parametro separator...sempre che il separatore fra i numeri non sia composto da diversi caratteri con spazi nel mezzo... Quindi a seconda delle specifiche del problema la tua potrebbe andare meglio di quella che ho scritto sotto. Se il formato è sempre quello che mi hai scritto "3 > 10" però va sempre bene. Codice:
vector<int> LLUtils::GetMinToMaxValues(const string& s)
{
int min, max;
string separator;
istringstream iss(s);
iss >> min >> separator >> min;
for ( int i = Min; i <= Max; ++i )
v.push_back(i); // creates the list
return v;
}
|
|
|
|
|
|
#15 | |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
Ho Min to Max, Min & Max, e valori multipli separati da | Però grazie, interessante
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
|
#16 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Fammi un esempio di valori multipli...
|
|
|
|
|
|
#17 |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
non ne abbiamo ancora ma la settimana prossima arriveranno i dati di configurazione di un dispositivo radio che dovrà far parte della comunicazione.
Ma da quello che ho capito sono solo interi quindi la stringa potrebbe essere qualcosa come: "1 | 3 | 12 | 45" ovviamente possono essere 1 così come 10.000 ... Pensavo di aver già fatto la funzione invece non la trovo ... mi sa che devo dormire un pò di più e lavorare un pò di meno ... EDIT: ah ecco, avevo fatto qualcosa trovando degli esempi STL in internet: Codice:
bool LLElementHandler::GetFields( string & text, string & separators, list<string> &words )
{
LogL.Write( LogLib::LOGLIB_DEBUG_FLOW_MSG, LOGLIB_FIX_ARGS, 0, "" );
int n = text.length();
int start, stop;
start = text.find_first_not_of(separators);
while ((start >= 0) && (start < n))
{
stop = text.find_first_of(separators, start);
if ((stop < 0) || (stop > n))
stop = n;
words.push_back( text.substr(start, stop - start) );
start = text.find_first_not_of(separators, stop + 1);
}
return true;
}
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve Ultima modifica di trallallero : 04-01-2008 alle 11:45. |
|
|
|
|
|
#18 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Se sono separati da spazio e da un altro carattere:
Codice:
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
vector<int> parse(const string &s)
{
istringstream iss(s);
vector<int> v;
string separator;
while (!iss.fail())
{
int tmp;
iss >> tmp;
if (!iss.fail()) v.push_back(tmp);
iss >> separator;
}
return v;
}
int main()
{
vector<int> v = parse("1 | 3 | 5 | 5");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
cout << *it << " ";
cout << endl;
v = parse("1 < 5");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
cout << *it << " ";
cout << endl;
return 0;
}
|
|
|
|
|
|
#19 |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
in pratica se non da errore è un intero e lo copi in list, l'ultimo errore fa uscire.
Interessante ... in C è più corto ed elegante Codice:
v.push_back(atoi(c)); while( c = strchr(c, Separator) ) v.push_back(atoi(++c)); Insomma con 'sto STL se pò fà tutto ... mi devo solo abituare ad usarlo ... addio C
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
#20 |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
ultimissima, lo giuro
![]() sto applicando ciò che m'hai scritto (template) ma adesso che non so che tipo è, come faccio a estrapolare i dati ? Ovviamente non posso più fare atoi, quindi ? Codice:
template <class T> vector<T> LLUtils::GetMinToMaxValues( string& s, int Separator )
{
vector<T> v;
int Min = atoi( s.c_str() );
int Max = atoi( strchr(s.c_str(), Separator) + 1 );
for ( int i = Min; i <= Max; ++i )
v.push_back(i);
return v;
}
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 21:53.












)







