std::vector<std::string> in char* array

std::vector<std::string> in char* array


Ho un std::vector<std::string> che devo usare per un C argomento della funzione che legge char* foo . Ho visto come convertire un std::string a char* . Come nuovo arrivato in C++ , sto cercando di ricostruire come eseguire questa conversione su ciascun elemento del vettore e produrre il char* matrice.


Ho visto diverse domande SO strettamente correlate, ma la maggior parte sembra illustrare modi per andare nell'altra direzione e creare std::vector<std::string> .


Risposte:


Puoi usare std::transform come:


std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);  

Ciò richiede l'implementazione di convert() come:


char *convert(const std::string & s)
{
char *pc = new char[s.size()+1];
std::strcpy(pc, s.c_str());
return pc;
}


Codice di prova:


int main() {
std::vector<std::string> vs;
vs.push_back("std::string");
vs.push_back("std::vector<std::string>");
vs.push_back("char*");
vs.push_back("std::vector<char*>");
std::vector<char*> vc;
std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);
for ( size_t i = 0 ; i < vc.size() ; i++ )
std::cout << vc[i] << std::endl;
for ( size_t i = 0 ; i < vc.size() ; i++ )
delete [] vc[i];
}

Uscita:


std::string
std::vector<std::string>
char*
std::vector<char*>

Demo online:http://ideone.com/U6QZ5


Puoi usare &vc[0] ovunque tu abbia bisogno di char** .


Nota che poiché stiamo usando new allocare memoria per ogni std::string (in convert funzione), dobbiamo deallocare la memoria alla fine. Questo ti dà la flessibilità di cambiare il vettore vs; puoi push_back più stringhe, elimina quella esistente da vs e vc (ovvero vector<char*> sarà ancora valido!


Ma se non vuoi questa flessibilità, puoi usare questo convert funzione:


const char *convert(const std::string & s)
{
return s.c_str();
}

E devi cambiare std::vector<char*> a std::vector<const char*> .


Ora, dopo la trasformazione, se modifichi vs inserendo nuove stringhe, oppure eliminando da essa quelle vecchie, quindi tutte le char* in vc potrebbe diventare non valido. Questo è un punto importante. Un altro punto importante è che non è necessario utilizzare delete vc[i] nel tuo codice più.


Alcune risposte al codice


std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);
char *convert(const std::string &
s) { char *pc = new char[s.size()+1];
std::strcpy(pc, s.c_str());
return pc;
}
int main() {
std::vector<std::string>
vs;
vs.push_back("std::string");
vs.push_back("std::vector<std::string>");
vs.push_back("char*");
vs.push_back("std::vector<char*>");
std::vector<char*>
vc;
std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);for ( size_t i = 0 ;
i <
vc.size() ;
i++ ) std::cout <<
vc[i] <<
std::endl;
for ( size_t i = 0 ;
i <
vc.size() ;
i++ ) delete [] vc[i];
}
std::string std::vector<std::string>
char* std::vector<char*>
const char *convert(const std::string &
s) { return s.c_str();
}
std::vector<const char *>
cStrArray;
cStrArray.reserve(origVector.size());
for(int index = 0;
index <
origVector.size();
++index) { cStrArray.push_back(origVector[index].c_str());
} //NO RESIZING OF origVector!!!! SomeCFunction(&cStrArray[0], cStrArray.size());
char ** arr = new char*[vec.size()];
for(size_t i = 0;
i <
vec.size();
i++){
arr[i] = new char[vec[i].size() + 1];
strcpy(arr[i], vec[i].c_str());
}
for(size_t i = 0;
i <
vec.size();
i++){
delete [] arr[i];
} delete [] arr;
std::vector<std::string>
strings = /* from somewhere */;
int nterms = /* from somewhere */;
// using std::transform is a possibility depending on what you want // to do with the result of the call std::for_each(strings.begin(), string.end(), [nterms](std::string&
s) { ModelInitialize(&s[0], nterms);
}
s = std::string(s.begin(), std::find(s.begin(), s.end(), '\0'));
typedef std::unique_ptr<char[]>
pointer;
std::vector<pointer>
args;
std::transform(strings.begin(), strings.end() , std::back_inserter(args) , [](std::string const&
s) ->
pointer {
pointer p(new char[s.size()]);
std::copy(s.begin(), s.end(), &p[0]);
return p;
});
std::for_each(args.begin(), args.end(), [nterms](pointer&
p) { ModelInitialize(p.get(), nterms);
});
const char*a="something";
////whatever it is here const char* retfunc(const char*a) { char*temp=a;
//process then return temp }
std::vector<char>
v;
char* c = &v[0];

No