2
可能重複:
Is it possible to get the pointer the continous memory fragment in a std::vector<char> in C++?的char * :: vector的<char>
我如何從std::vector<char>
char*
,就像函數std::string.c_str()
?
可能重複:
Is it possible to get the pointer the continous memory fragment in a std::vector<char> in C++?的char * :: vector的<char>
我如何從std::vector<char>
char*
,就像函數std::string.c_str()
?
採取的第一個元素的地址:
char * foo = &my_vec[0];
要在space_cowboy的回答略有擴大......
你可以這樣做:
std::vector<char> vec;
vec.push_back('a');
vec.push_back('b');
vec.push_back('c');
char *arr = &vec[0];
但是,如果你現在是到做一個push_back
...
vec.push_back('d');
arr; // This pointer has been possibly invalidated!
// realloc could have been called on vec's memory.