是的,stringstream可以用于數(shù)字和字符串之間的轉(zhuǎn)換??梢允褂胹tringstream的<<操作符將數(shù)字轉(zhuǎn)換為字符串,也可以使用>>操作符將字符串轉(zhuǎn)換為數(shù)字。以下是一個簡單的示例:
#include <iostream>
#include <sstream>
int main() {
// 將數(shù)字轉(zhuǎn)換為字符串
int num = 123;
std::stringstream ss;
ss << num;
std::string str = ss.str();
std::cout << "Number converted to string: " << str << std::endl;
// 將字符串轉(zhuǎn)換為數(shù)字
std::string strNum = "456";
int convertedNum;
ss.clear();
ss.str(strNum);
ss >> convertedNum;
std::cout << "String converted to number: " << convertedNum << std::endl;
return 0;
}
運行上述代碼將輸出:
Number converted to string: 123
String converted to number: 456