c++涓璼tringstream濡備綍搴旂敤
stringstream鏄竴涓敤浜庡瓧绗︿覆鎿嶄綔鐨勭被锛屽彲浠ュ皢鍚勭鏁版嵁绫诲瀷杞崲涓哄瓧绗︿覆锛屼篃鍙互灏嗗瓧绗︿覆杞崲涓哄悇绉嶆暟鎹被鍨嬨€備笅闈㈡槸涓€浜泂tringstream鐨勫父瑙佺敤娉?
- 灏嗗悇绉嶆暟鎹被鍨嬭浆鎹负瀛楃涓?
#include <iostream>
#include <sstream>
int main() {
int num = 123;
double value = 3.14;
std::stringstream ss;
ss << "Integer: " << num << ", Double: " << value;
std::string str = ss.str();
std::cout << str << std::endl;
return 0;
}
- 灏嗗瓧绗︿覆杞崲涓哄悇绉嶆暟鎹被鍨?
#include <iostream>
#include <sstream>
int main() {
std::string str = "123 3.14";
int num;
double value;
std::stringstream ss(str);
ss >> num >> value;
std::cout << "Integer: " << num << ", Double: " << value << std::endl;
return 0;
}
- 娓呯┖stringstream:
#include <iostream>
#include <sstream>
int main() {
std::stringstream ss;
ss << "Hello, World!";
std::cout << ss.str() << std::endl;
ss.str("");
std::cout << ss.str() << std::endl; // Output is empty
return 0;
}
- 閫氳繃stringstream杩涜鏍煎紡鍖栬緭鍑?
#include <iostream>
#include <sstream>
#include <iomanip>
int main() {
double value = 3.14159;
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << value;
std::string str = ss.str();
std::cout << "Formatted Value: " << str << std::endl;
return 0;
}
浠ヤ笂鏄痵tringstream鐨勪竴浜涘父瑙佺敤娉曪紝鍙互鏍规嵁瀹為檯闇€姹傜伒娲昏繍鐢╯tringstream绫昏繘琛屽瓧绗︿覆鎿嶄綔銆?/p>