指定内存创建对象
#include ? <iostream>?
#include ? <new>?
#include ? <cstdlib>?
using ? namespace ? std;?
int ? main()?
{?
? ? char* ? buf ? = ? new ? char[sizeof(string)]; ? // ? 分配空間?
? ? string* ? pstr ? = ? new(buf) ? string( "abc "); ? // ? 在所給空間上應用構造函數創建對象?
? ? //cout < <buf;?
? ? cout < <*pstr;?
? ? system( "pause ");?
}
#include ? <new>?
#include ? <cstdlib>?
using ? namespace ? std;?
int ? main()?
{?
? ? char* ? buf ? = ? new ? char[sizeof(string)]; ? // ? 分配空間?
? ? string* ? pstr ? = ? new(buf) ? string( "abc "); ? // ? 在所給空間上應用構造函數創建對象?
? ? //cout < <buf;?
? ? cout < <*pstr;?
? ? system( "pause ");?
}
總結
- 上一篇: USACO 3.2 Stringsobi
- 下一篇: 称球问题