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