Possible en double:
Is it possible to get the pointer the continous memory fragment in a std::vector<char> in C++?Char * de std :: vector <char>
Comment puis-je obtenir char*
de std::vector<char>
, tout comme la fonction std::string.c_str()
?
Possible en double:
Is it possible to get the pointer the continous memory fragment in a std::vector<char> in C++?Char * de std :: vector <char>
Comment puis-je obtenir char*
de std::vector<char>
, tout comme la fonction std::string.c_str()
?
Prenez l'adresse du premier élément:
char * foo = &my_vec[0];
Pour développer un peu sur la réponse space_cowboy ...
Vous pouvez faire:
std::vector<char> vec;
vec.push_back('a');
vec.push_back('b');
vec.push_back('c');
char *arr = &vec[0];
Cependant, si vous deviez maintenant faire un push_back
...
vec.push_back('d');
arr; // This pointer has been possibly invalidated!
// realloc could have been called on vec's memory.