
以字符串形式存储的数字和以数字形式存储的数字之间是有区别的。
例如,字符串 "2679" 就不是一个数字:它是由 2、6、7、9 这 4 个字符的 ASCII 码组成的序列。由于字符串 "2679" 不是一个数字,所以编译器将不允许对它进行加法、乘法和除法之类的数学运算。以数字表示的字符串必须首先转换为数字形式,然后才能与算术运算符一起使用。
请编写一程序,该程序的功能是首先将用户通过键盘输入的若干字符(用eof结束输入)存入一维数组s中,然后找出数组中具有最大ascii码值的字符,并且输出该字符以及该字符对应的ascii码。1:以字符形式输入,单个字符数出(可以实现大小写,数字和符号的收发)2:以十六进制形式输入c++ 字符串截取,十六进制输出(可以实现大小写,数字和符号的收发)其中,49 20 67 74 20 分别代表i,空格,g,e,t,空格。下列程序的功能是从键盘上输入若干个字符(以回车键为结束)组成一个字符串存入一个字符数组,然后输出该数组中的字符串,请补充全程序画线的部分。
当用户在键盘上输入一个数字时,该数字以字符串的形式输入,就好像用户输入的一系列字符(数字)。在 C++ 中,这样的数字通常通过流提取运算符 >> 来读取。在存储到数字类型的变量之前c++ 字符串截取,该运算符会根据需要自动执行转换。在输出期间,数字到字符串的反向转换由流输出运算符 << 执行。
使用字符串流对象进行数字转换C++ 有两个类,ostringstream 和 istringstream,可以用来对内存中的值执行字符串/数字转换。
ostringstream 类是 ostream 的子类(cout 也属于该类),并使用流插入运算符 << 将数值转换为字符串。ostringstream 类型对象的工作方式与cout和文件对象的工作方式相同,但它不是将数据写入屏幕或文件,而是写入它所包含的字符串对象中。
每次在 ostringstream 对象上使用 << 时,它都会执行必要的数字到字符串的转换,并将结果追加到其字符串的末尾。除了支持 ostream 类的所有成员函数和运算符外,ostringstream 对象还支持表 1 中所示的 str 成员函数。
istringstream 类是从 istream 派生的。它内部包含一个字符串对象,函数将它作为可以从中"读取"的输入流。
输入流可以在创建对象时由 istringstream 构造函数设置,也可以在创建对象后通过调用 str(string s) 函数来设置。流提取操作符 >> 从封闭的字符串中读取,并在必要时将字符串转换为数字。表 1 列出了 istringstream 的成员函数,必须在程序中包含 sstream 头文件才能使用这些类。
表 1 ostringstream 和 istringstream 类的成员函数成员函数描述
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 对象,只有这样才能使用它们的插入和提取运算符执行转换。
数字转换函数C++ 11 提供了若干 to_string(T value) 函数来将 T 类型的数字值转换为字符串形式。以下是几个 to_string() 函数的列表:
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
然后转换结果为整数,并同时按位右移第一个操作及该整数,直到第一个操作数的二进制个位数字为1,这时整数按位右移的结果以无符号10进制显示出来。hashbytes() 函数的返回结果是 varbinary 型,也就是以 0x 开头 16 进制形式的二进制数据,不过通常情况下,我们需要的都是字符串型的数据,很首先想到的可能就是用 cast 或 convert 函数将varbinary 转换为 varchar,但这样转换后的结果会是乱码,正确转换 varbinary 可变长度二进制型数据到 16 进制字符串应该使用系统内置函数 sys.fn_varbintohexstr(),如下所示:。备注:无论是 java 还是 javascript, parseint 方法都有两个参数, 第一个参数就是要转换的对象, 第二个参数是进制基数, 可以是 2, 8, 10, 16, 默认以 10 进制处理. 但在 javascript 中, 0 开始的数被认为使用 8 进制处理, 0x 的数被认为是用 16 进制来处理。
字符串到数字的转换可以通过 stoX() 系列函数来执行。该系列函数的成员可以将字符串转换为 int、long、float 和 double 类型的数字。具体语法如下所示:
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)
按照以前两个字符串找两者的最长公共子串的思路类似,可以把所有串拼接到一起,这里为了避免讨论lcp跨越多个串需需要特别处理的问题用不同的字符把所有串隔开(因为char只有128位,和可能不够用,更推荐设置成某一特殊字符,在后缀数组初始化的时候在对其映射的int值做处理)。 //定义i为循环次数,j来存放转换后的整数,n用来统计该数组中有几个数字字符。strtoul() 会扫描参数 str 字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 isspace() 函数来检测),直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('。
例如,如果试图转换字符串 "-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 <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
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-105345-1.html
就一艘破军舰
人家的确是在公海航行
是狗天性多自负
关键是我们抓紧时间好好发展