[英]What is the most efficient way to store a char pointer into a string in C++
[英]What is the most efficient way to convert a string to a char array/ value that a char pointer can point to
如果我有一個字符串:
std::string Pooptacular = "Pooptacular"
我想將其轉換為char數組,我有一些選擇:
char* poopCArr = Pooptacular.c_str();
或者我可以用memcpy或strcpy之類的東西做些事情。
我想知道的是,哪種方法最快或最有效。 換句話說,如果我要在一個程序運行中執行數十萬次此操作,應該使用哪種方法?
如果只需要指向數據的只讀指針,請使用c_str
。 它不會轉換任何東西。 它只是讓您訪問已分配的緩沖區string
。 當然,如果要更改它,則必須將其復制到新的緩沖區中;如果這樣做,則不要期望您的更改會反映在原始string
對象中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.