I’m trying to assign char* values into a queue from a loop. I keep copying the pointer rather than assigning the data itself. I feel like there’s a simple answer, but I can;t find a good example.
Example
while(something) {
next = queue.front();
queue.pop();
while(something) {
/* do work */
/* text has new value of char* */
queue.push(text);
}
}
This doesn’t work, obviously, as when I assign a new value to text, all of the entries in queue become that new value. I need to know how to do the proper copy/assignment.
Use
std::queue<std::string>instead andstd::stringfor keeping strings.If you are getting a
char *from somewhere, you can push it to the queue of typestd::queue<std::string>and the string will be constructed implicitly.If you cannot use a
std::stringfor any reason (although, I cannot think about any good one for simple cases), you have to:strnlen).