Reputation: 202
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.
Upvotes: 1
Views: 1649
Reputation: 22690
Use std::queue<std::string>
instead and std::string
for keeping strings.
If you are getting a char *
from somewhere, you can push it to the queue of type std::queue<std::string>
and the string will be constructed implicitly.
If you cannot use a std::string
for any reason (although, I cannot think about any good one for simple cases), you have to:
strnlen
).Upvotes: 5