'std::to_string return empty string when using std::allocator

I got the empty return of std::to_string when I use std::allocator. The code is:

#include <iostream>
#include <memory>
#include <string>

int main()
{
    std::allocator<std::string> allocatorStr;
    auto const pStr = allocatorStr.allocate(5);
    for (int i = 0; i < 5; ++i) { *(pStr + i) = std::to_string(i); }
    for (int i = 0; i < 5; ++i) { std::cout << *(pStr + i) << std::endl; }

    return 0;
}

But if I change the line *(pStr + i) = std::to_string(i);to *(pStr + i) = "string"; I could get non-empty output. I don't know where is wrong. Thank you very much for your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source