use*_*ser 5 c++ tostring stringstream c++11
下面的代码显示2种溶液(std::to_string和std::stringstream),该转换的int m_currentSoundTime到std::string.是更快std::to_string还是std::stringstream更快?
// Compute current sound time in minute and convert to string
stringstream currentTime;
currentTime << m_currentSoundTime / 60;
m_currentSoundTimeInMinute = currentTime.str();
Run Code Online (Sandbox Code Playgroud)
要么
m_currentSoundTimeInMinute = to_string( m_currentSoundTime / 60 );
Run Code Online (Sandbox Code Playgroud)
在任何合理的库实现to_string将至少快stringstream了这一点.但是,如果您想将10个整数放入字符串中,stringstream则可能会更快.如果你要做的to_string(a) + ", " + to_string(b) + /*...*/每一个操作可能会导致分配和从前一个字符串到新分配的副本 - 不是真的stringstream.
更重要的是,从您的示例代码中可以清楚地看出to_string,将单个 int转换为字符串更简洁.
| 归档时间: |
|
| 查看次数: |
7029 次 |
| 最近记录: |