I am an experienced coder, but I'm still relatively new to STL, and just ran into this problem:
As far as I know, STL containers are not designed to copy objects that they contain, or otherwise affect their life cycles, but experimentally I see different results.
In particular, string classes that are designed to nullify the first character of their base storage upon destruction are still available if they are stored in the container before they go out of scope. For example, consider the following example:
using the std namespace;
queue<string> strQueue;
const char *genStr(int i)
{
ostringstream os;
os << "The number i is " << i;
strQueue.push(os.str());
return strQueue.back().data();
}
void useStr()
{
while(!strQueue.empty())
{
cout << strQueue.front() << endl;
strQueue.pop();
}
}
int main(int argc, char **argv)
{
for(int i = 0; i < 40; i++)
{
printf("Retval is: %s\n", genStr(i));
}
useStr();
return 0;
}
, genStr() , , printf "Retval is:" , , useStr(), undefined, , .
, , , , .