sstream 头文件才能使用这些类。| 成员函数 | 描 述 |
|---|---|
| istringstream(string s) |
istringstream 的构造函数:设置对象的输入流的初始值。示例: istringstream istr ("50 64 28"); |
| ostringstream(string s) |
ostringstream 的构造函数:设置对象的输出流的初始值。示例: ostringstream ostr("50 64 28"); |
| string str() |
返回 ostringstream 或 istringstream 对象中包含的字符串。示例: string is = istr.str(); string os = ostr.str (); |
| void str(string &s) |
设置作为对象的输入或输出流的字符串。示例: ostr.str ("50 64 28"); istr.str("50 64 28"); |
//This program illustrates the use of sstream objects
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "John 20 50"; // string to read from
const char *cstr = "Amy 30 42"; // Cstring to read from
istringstream istr1 (str); // istr1 will read from str
istringstream istr2; // istr2 will read from cstr
ostringstream ostr; // The ostringstream object to write to
string name;
int score1, score2, average_score;
// Read name and scores and compute average then write to ostr
istr1 >> name >> score1 >> score2;
average_score = (score1 + score2)/2;
ostr << name << " has average score" << average_score << "\n";
// Set istr2 to read from the C string and repeat the above
istr2.str(cstr);
istr2 >> name >> score1 >> score2;
average_score = (score1 + score2)/2;
ostr << name << " has average score" << average_score << "\n";
// Switch to hexadeximal output on ostr
ostr << hex;
// Write Amy's scores in hexadecimal
ostr << name << "'s scores in hexadecimal are: " << score1 << " and " << score2 << "\n";
// Extract the string from ostr and print it to the screen
cout << ostr.str();
return 0;
}
程序输出结果:
John has average score35
Amy has average score36
Amy's scores in hexadecimal are: 1e and 2a
string to_string(int value)
string to_string(long value)
string to_string(double value)
int a = 5; string str = to_string(a*a); cout << " The square of 5 is " << str << endl;以上示例即显示了该系列函数的用法。它将打印如下字符串:
The square of 5 is 25
to_string() 函数无法处理非十进制整数的转换。如果需要该功能,则应该使用 ostringsteam 对象来完成该转换。
int stoi(const strings str, size_t* pos = 0, int base = 10)
long stol(const strings str, size_t* pos = 0, int base = 10)
float stof(const strings str, size_t* pos = 0)
double stod(const strings str, size_t* pos = 0)
// This program demonstrates the use of the stoXXX()
// numeric conversion functions.
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str; // string to convert
size_t pos; // Hold position of stopping character
//Convert string to double
str = "-342.57is a number";
cout << "The string is " << str << endl;
double d = stod(str, &pos);
cout << "The converted double is " << d << endl;
cout << "The stopping character is " << str[pos] << " at position " << pos << endl;
// Convert string to int (default to decimal)
str = "-342.57is a number";
cout << "\nThe string is " << str << endl;
int i = stoi(str, &pos);
cout << "The converted integer is " << i << endl;
cout << "The stopping character is " << str[pos] <<" at position " << pos << endl;
// Convert string to int (base is binary)
str = "01110binary number";
cout << "\nThe string is " << str << endl;
i = stoi (str, &pos, 2);
cout << "The converted binary integer is " << i << endl;
cout << "The stopping character is " << str[pos] << " at position " << pos << endl;
return 0;
}
程序输出结果:
The string is -342.57is a number
The converted double is -342.57
The stopping character is i at position 7
The string is -342.57is a number
The converted integer is -342
The stopping character is . at position 4
The string is 01110binary number
The converted binary integer is 14
The stopping character is b at position 5
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有