stringstream

C++数值与字符串转换

匿名 (未验证) 提交于 2019-12-03 00:38:01
C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含sstream头文件。 istringstream类用于执行C风格的串流的输入操作。 ostringstream类用于执行C风格的串流的输出操作。 stringstream类同时可以支持C++风格的串流的输入输出操作。 下图详细描述了几种类之间的继承关系: 简单的str和num(int类型)转换直接可以用stringstream类来实现,代码如下: #include <sstream> #include <string> string num2str( double i) //数字转字符串 { stringstream ss; ss<<i; return ss.str(); } int str2num( string s) //字符串转数字 { int num; stringstream ss(s); ss>>num; return num; } 也可以使用ostringstream将 字符串 转换为 数值 : #include <iostream> #include <sstream> #include <string> using namespace std ; template < class T> inline string toString(

sstream库的使用

匿名 (未验证) 提交于 2019-12-03 00:18:01
<sstream> 库定义了三种类: istringstream、ostringstream和stringstream ,分别用来进行 string 流的输入、输出和输入输出操作。 另外,每个类都有一个对应的宽字符集版本。 简单起见,主要以 stringstream 为中心,因为每个转换都要涉及到输入和输出操作。 注意, <sstream> 使用 string 对象来代替字符数组。 这样可以避免缓冲区溢出的危险。 而且,传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险。 string result=” 10000 ”; int n= 0 ; stream<<result; stream>>n; //n等于10000 重复利用 stringstream 对象 如果你打算在多次转换中使用同一个 stringstream 对象,记住再每次转换前要使用 clear() 方法; 在多次转换中重复使用同一个 stringstream (而不是每次都创建一个新的对象)对象最大的好处在于效率。 stringstream 对象的 构造和析构函数 通常是非常耗费CPU时间的。 在类型转换中使用模板 你可以轻松地定义函数模板来将一个任意的类型转换到特定的目标类型。例如,需要将各种数字值,如 int、long、double 等等转换成字符串,要使用以一个 string

sstream

匿名 (未验证) 提交于 2019-12-02 23:43:01
stringstream则是 初始化方法: stringstream stream; int a; stream << "12"; stream >> a; cout << a << endl; stringstream stream("adfaf afagfagag"); string a; stream >> a; cout << a << endl; string a = "aljfljfalf"; stringstream stream(a); cout << stream.str() << endl; 1 #include <sstream> 2 using namespace std; 3 int main() 4 { 5 stringstream stream; 6 int a,b; 7 stream << "080";//将“080”写入到stream流对象中 8 stream >> a;//将stream流写入到a中,并根据a的类型进行自动转换,"080" -> 80 9 cout << stream.str() << endl;//成员函数str()返回一个string对象,输出80 10 cout << stream.length() << endl;//2 11 } str()和clear() stream用完之后,其内存和标记仍然存在

stringstream istringstream ostringstream 三者的区别

牧云@^-^@ 提交于 2019-12-02 22:56:25
stringstream istringstream ostringstream 三者的区别 说明 ostringstream : 用于执行C风格字符串的输出操作。 istringstream : 用于执行C风格字符串的输入操作。 stringstream : 同时支持C风格字符串的输入输出操作。 通常,ostringstream 类用来格式化字符串,避免申请大量的缓冲区,替代sprintf。该类能够根据内容自动分配内存,其对内存管理也是相当到位。 原文链接 代码示例 #include <string> #include <sstream>// #include <iostream> using namespace std; //ostringstream 用于执行C风格字符串的输出操作 void ostringstream_test() { //ostringstream 只支持 << 操作符 std::ostringstream oss; oss << "this is test" << 123456; std::cout<<oss.str()<<std::endl; oss.str("");//清空之前的内容 //oss.clear();//并不能清空内存 //浮点数转换限制 double tmp = 123.1234567890123; oss.precision(12);

Writing stringstream contents into ofstream

最后都变了- 提交于 2019-12-02 19:52:37
I'm currently using std::ofstream as follows: std::ofstream outFile; outFile.open(output_file); Then I attempt to pass a std::stringstream object to outFile as follows: GetHolesResults(..., std::ofstream &outFile){ float x = 1234; std::stringstream ss; ss << x << std::endl; outFile << ss; } Now my outFile contains nothing but garbage: "0012E708" repeated all over. In GetHolesResults I can write outFile << "Foo" << std:endl; and it will output correctly in outFile . Any suggestion on what I'm doing wrong? You can do this, which doesn't need to create the string. It makes the output stream read

C++ 中的 stringstream 类

徘徊边缘 提交于 2019-12-02 18:09:47
<sstream>定义了三个类:istringstream,ostringstream,stringstream,分别用来进行流的输入、输出、输入输出操作。 <sstream>主要用来数据类型转换,<sstream>使用string对象代替字符数组snprintf,避免缓冲区溢出,其传入参数和目标对象的类型会被自动推导出来,因此不存在错误的格式化符的问题。安全、自动、直接。 数据类型转换: #include<string> #include<iostream> #include<sstream> #include<stdio.h> using namespace std; //数据类型转换 int main(){ stringstream sstream; string strResult; int nValue=1000; sstream<<nValue;//将int型的放到输入流 sstream>>strResult;//从流中拿出来赋给string类型 cout<<"cout strResult is:"<<strResult<<endl; printf("printf strResult is:%s",strResult.c_str()); return 0; } 结果: cout strResult is:1000 printf strResult is:1000

Linking error using Arduino library with c++'s stringstream

£可爱£侵袭症+ 提交于 2019-12-02 14:23:05
问题 I am using an esp8266 together with platformio to write a simple sketch. #include <sstream> #include <Arduino.h> std::stringstream s; void setup() { Serial.begin(9600); Serial.println("Test"); } void loop() { } Everything should be setup correctly and most sketches worked without issues. But as soon as I try to use stringstreams I get errors when platformio is linking the firmware. platformio run [12/29/16 12:11:32] Processing esp12e (platform: espressif8266, board: nodemcu, framework:

Using a character array as a string stream buffer

…衆ロ難τιáo~ 提交于 2019-12-02 02:35:47
I'm looking for a clean STL way to use an existing C buffer (char* and size_t) as a string stream. I would prefer to use STL classes as a basis because it has built-in safeguards and error handling. note: I cannot use additional libraries (otherwise I would use QTextStream ) You can try with std::stringbuf::pubsetbuf . It calls setbuf , but it's implementation defined whether that will have any effect. If it does, it'll replace the underlying string buffer with the char array, without copying all the contents like it normaly does. Worth a try, IMO. Test it with this code: std::istringstream

stringstream unsigned conversion broken?

流过昼夜 提交于 2019-12-01 21:03:03
问题 Consider this program: #include <iostream> #include <string> #include <sstream> #include <cassert> int main() { std::istringstream stream( "-1" ); unsigned short n = 0; stream >> n; assert( stream.fail() && n == 0 ); std::cout << "can't convert -1 to unsigned short" << std::endl; return 0; } I tried this on gcc (version 4.0.1 Apple Inc. build 5490) on OS X 10.5.6 and the assertion is true; it fails to convert -1 to an unsigned short. In Visual Studio 2005 (and 2008) however, the assertion

istringstream decimal integer input to 8-bit type

本小妞迷上赌 提交于 2019-12-01 19:52:54
This: #include <iostream> #include <sstream> #include <inttypes.h> using namespace std; int main (void) { istringstream iss("123 42"); int8_t x; while (iss >> x) { cout << x << endl; } return 0; } Produces: 1 2 3 4 2 But I want: 123 42 Casting iss >> (int)x (I initially tried this with a char ) gives me " error : invalid operands to binary expression ('istringstream' (aka 'basic_istringstream') and 'int')" (clang) or " error : ambiguous overload for ‘operator>>’" (g++). Is there a way to read the value as a number directly into an 8-bit type, or do I have to use an intermediary store? You have