c语言库函数的一些类型转换:
sprintf()函数可以将一个变量从int类型转换到字符串类型。但是为了正确地完成这个任务,必须确保证目标缓冲区有足够大空间以容纳转换完的字符串。此外,还必须使用正确的格式化符。如果使用了不正确的格式化符,会导致非预知的后果。下面是一个例子:
int n=10000;
chars[10];
sprintf(s,”%d”,n);// s中的内容为“10000”
到目前为止看起来还不错。但是,对上面代码的一个微小的改变就会使程序崩溃:
int n=10000;
char s[10];
sprintf(s,”%f”,n);// 看!错误的格式化符
使用sprintf()的一个例子:
#include <stdio.h>
#include <string.h>
int main()
{
int abc, de, x, y, z, i, ok, count = 0;
char s[20], buff[100];
scanf("%s", s);
for (abc = 100; abc < 999; abc++)
{
for (de = 10; de < 99; de++)
{
x = abc * (de % 10);
y = abc * (de / 10);
z = abc * de;
sprintf(buff, "%d%d%d%d%d", abc, de, x, y, z);
ok = 1;
for (i = 0; i < strlen(buff); i++)
if (strchr(s, buff[i]) == NULL)
ok = 0;
if (ok)
{
printf("<%d>/n", ++count);
printf("%5d/nX%4d/n-----/n%5d/n%4d/n-----/n%5d/n", abc, de, x, y, z);
}
}
}
printf("The number of solutions = %d/n", count);
return 0;
}
sprintf()函数将abc,de,x,y,z转换为字符类型的每一个每一个字符。
c++的类型转换实现:
#include <sstream>
<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。
istringstream类用于执行C++风格的字符串流的输入操作。
ostringstream类用于执行C++风格的字符串流的输出操作。
strstream类同时可以支持C++风格的串流的输入输出操作。
(一)stringstream:
例子一:基本数据类型转换例子 int转string
# include <sstream>
# include <iostream>
int main()
{
std::stringstream stream;
std:: string result;
int i = 1000;
stream << i; // 将int输入流
stream >> result; // 从stream中抽取前面插入的int值
std::cout << result << std::endl; // print the string "1000"
}
运行结果:
例子二:除了基本类型的转换,也支持char *的转换。
# include <iostream>
int main()
{
std::stringstream stream;
char result[8] ;
stream << 8888; // 向stream中插入8888
stream >> result; // 抽取stream中的值到result
std::cout << result << std::endl; // 屏幕显示 "8888"
}
例子三:再进行多次转换的时候,必须调用stringstream的成员函数clear().
# include <iostream>
int main()
{
std::stringstream stream;
int first, second;
stream<< "456"; // 插入字符串
stream >> first; // 转换成int
std::cout << first << std::endl;
stream.clear(); // 在进行多次转换前,必须清除stream
stream << true; // 插入bool值
stream >> second; // 提取出int
std::cout << second << std::endl;
}
运行clear的结果
没有运行clear的结果
描述:从流中提取数据,支持 >> 操作
这里字符串可以包括多个单词,单词之间使用空格分开
- istringstream的构造函数原形:
- istringstream::istringstream(string str);
初始化:使用字符串进行初始化
- istringstream istr("1 56.7");
- istr.str("1 56.7");//把字符串"1 56.7"存入字符串流中
使用:我们可以使用分解点获取不同的数据,完成 字符串 到 其他类型 的转换
常用成员函数:
- str():使istringstream对象返回一个string字符串
举例:把字符串类型的数据转换为其他类型
- #include <iostream>
- #include <sstream>
- using namespace std;
- int main()
- {
- istringstream istr("1 56.7");
- cout<<istr.str()<<endl;//直接输出字符串的数据 "1 56.7"
- string str = istr.str();//函数str()返回一个字符串
- cout<<str<<endl;
- int n;
- double d;
- //以空格为界,把istringstream中数据取出,应进行类型转换
- istr>>n;//第一个数为整型数据,输出1
- istr>>d;//第二个数位浮点数,输出56.7
- //假设换下存储类型
- istr>>d;//istringstream第一个数要自动变成浮点型,输出仍为1
- istr>>n;//istringstream第二个数要自动变成整型,有数字的阶段,输出为56
- //测试输出
- cout<<d<<endl;
- cout<<n<<endl;
- system("pause");
- return 1;
- }
举例2:把一行字符串放入流中,单词以空格隔开。之后把一个个单词从流中依次读取到字符串
- #include <iostream>
- #include <sstream>
- using namespace std;
- int main()
- {
- istringstream istr;
- string line,str;
- while (getline(cin,line))//从终端接收一行字符串,并放入字符串line中
- {
- istr.str(line);//把line中的字符串存入字符串流中
- while(istr >> str)//每次读取一个单词(以空格为界),存入str中
- {
- cout<<str<<endl;
- }
- }
- system("pause");
- return 1;
- }
输入:123 34 45
输出:
123 换行 34 换行 45
ostringstream类
描述:把其他类型的数据写入流(往流中写入数据),支持<<操作
- ostringstream的构造函数原形:
- ostringstream::ostringstream(string str);
初始化:使用字符串进行初始化
- ostringstream ostr("1234");
- ostr.str("1234");//把字符串"1234"存入字符串流中
举例:
- #include <iostream>
- #include <sstream>
- using namespace std;
- int main()
- {
- //初始化输出字符串流ostr
- ostringstream ostr("1234");
- cout<<ostr.str()<<endl;//输出1234
- ostr.put('5');//字符4顶替了1的位置
- cout<<ostr.str()<<endl;//输出5234
- ostr<<"67";//字符串67替代了23的位置,输出5674
- string str = ostr.str();
- cout<<str<<endl;
- system("pause");
- return 1;
- }