enc*_*nch 1 c c++ queue variable-assignment
我正在尝试将char*值从循环中分配到队列中.我一直在复制指针,而不是自己分配数据.我觉得这是一个简单的答案,但我能找到一个很好的例子.
例
while(something) {
next = queue.front();
queue.pop();
while(something) {
/* do work */
/* text has new value of char* */
queue.push(text);
}
}
Run Code Online (Sandbox Code Playgroud)
显然,这不起作用,因为当我为文本分配新值时,队列中的所有条目都成为新值.我需要知道如何正确复制/分配.
使用std::queue<std::string>而不是std::string保持字符串.
如果char *从某个地方获取,可以将其推送到类型的队列,std::queue<std::string>并且将隐式构造字符串.
如果你std::string出于任何原因不能使用a (虽然我不能考虑任何一个好的例子),你必须:
strnlen).