字符串和数字的相互转换
reverse函数用于反转在[first,last)范围内的顺序(包括first指向的元素,不包括last指向的元素),
reverse函数无返回值
2、C语言的输出格式说明
格式说明由“%”和格式字符组成,如%d%f等。它的作用是将输出的数据转换为指定的格式输出。格式说明总是由“%”字符开始的。 格式字符有d,o,x,u,c,s,f,e,g等。 如%d整型输出,%ld长整型输出,%o以八进制数形式输出整数,%x以十六进制数形式输出整数,或输出字符串的地址。%u以十进制数输出unsigned型数据(无符号数)。注意:%d与%u有无符号的数值范围,也就是极限的值,不然数值打印出来会有误。%.100f用来输出实数,保留小数点100位。%e以指数形式输出实数。%g根据大小自动选f格式或e格式,且不输出无意义的零。
3、使用字符串流对象进行数字转换
成员函数 | 描 述 |
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
请注意,这些类具有 ostream 和 istream 对象的全部功能,包括使用八进制和十六进制等不同进制将数字转换为字符串的能力。当然,它们也是有缺陷的,它们强制程序员创建 sstream 对象,只有这样才能使用它们的插入和提取运算符执行转换。
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)
第一个形参 str 是一个字符串(例如 "-342" 或 "3.48" 等),它将被转换为恰当的数字形式。这些函数可以将 str 可能的最长前缀转换为数字,并返回一个整数地址 pos,pos 中保存了 str 无法被转换的第一个字符的索引。类型 size_t 是在标准库中定义的,常用于表示无符号整数的大小或数组、矢量、字符串中的一个索引。
例如,如果试图转换字符串 "-34iseven",则将成功返回整数 -34,而无法转换的第一个字符的位置 pos 则被设置为 3。base 形参仅适用于整数转换,指示用于转换的进制。pos 和 base 形参都是可选的,所以它们可以被忽略。如果 pos 被忽略,则不会存储停止字符的索引。如果 base 被忽略,则默认为十进制。如果字符串 str 包含一个无效值,例如 "is-34 even?",则不会进行转换,函数将抛出一个 invalid_argument 异常。
// This program demonstrates the use of the stoXXX()
// numeric conversion functions.
#include
#include
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 converted binary integer is 14
The stopping character is b at position 5
string to_string(long value)
string to_string(double value)