强大的格式转换(利用c和c++的一些函数实现)

来源:互联网 发布:金蝶软件erp 编辑:程序博客网 时间:2024/06/11 19:12

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 <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"

 

 

运行结果:

001

 

例子二:除了基本类型的转换,也支持char *的转换。

 

#include <sstream>
#
include <iostream> 

int main()
{
    std::stringstream stream;
    char result[8] ;
    stream << 8888; //向stream中插入8888
    stream >> result; //抽取stream中的值到result
    std::cout << result << std::endl; // 屏幕显示 "8888"

 

 

002

 

例子三:再进行多次转换的时候,必须调用stringstream的成员函数clear().

 

#include <sstream>
#
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的结果

003

没有运行clear的结果

004

(二)istringstream

描述:从流中提取数据,支持 >> 操作

这里字符串可以包括多个单词,单词之间使用空格分开

  1. istringstream的构造函数原形:  
  2. istringstream::istringstream(string str);  

初始化:使用字符串进行初始化

  1. istringstream istr("1 56.7");  
  2. istr.str("1 56.7");//把字符串"1 56.7"存入字符串流中   

使用:我们可以使用分解点获取不同的数据,完成 字符串 到 其他类型 的转换

常用成员函数:

 

  1. str():使istringstream对象返回一个string字符串  

举例:把字符串类型的数据转换为其他类型

 

  1. #include <iostream>   
  2. #include <sstream>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     istringstream istr("1 56.7");  
  7.   
  8.     cout<<istr.str()<<endl;//直接输出字符串的数据 "1 56.7"   
  9.       
  10.     string str = istr.str();//函数str()返回一个字符串   
  11.     cout<<str<<endl;  
  12.       
  13.     int n;  
  14.     double d;  
  15.   
  16.     //以空格为界,把istringstream中数据取出,应进行类型转换   
  17.     istr>>n;//第一个数为整型数据,输出1   
  18.     istr>>d;//第二个数位浮点数,输出56.7   
  19.   
  20.     //假设换下存储类型   
  21.     istr>>d;//istringstream第一个数要自动变成浮点型,输出仍为1   
  22.     istr>>n;//istringstream第二个数要自动变成整型,有数字的阶段,输出为56   
  23.   
  24.     //测试输出   
  25.     cout<<d<<endl;  
  26.     cout<<n<<endl;  
  27.     system("pause");  
  28.     return 1;  
  29. }  

举例2:把一行字符串放入流中,单词以空格隔开。之后把一个个单词从流中依次读取到字符串

  1. #include <iostream>   
  2. #include <sstream>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     istringstream istr;  
  7.     string line,str;  
  8.     while (getline(cin,line))//从终端接收一行字符串,并放入字符串line中   
  9.     {  
  10.         istr.str(line);//把line中的字符串存入字符串流中   
  11.         while(istr >> str)//每次读取一个单词(以空格为界),存入str中   
  12.         {  
  13.             cout<<str<<endl;  
  14.         }  
  15.     }  
  16.     system("pause");  
  17.     return 1;  
  18. }  

输入:123 34 45

输出:

123  换行 34 换行 45



(三)ostringstream

ostringstream

描述:把其他类型的数据写入流(往流中写入数据),支持<<操作

  1. ostringstream的构造函数原形:  
  2. ostringstream::ostringstream(string str);  

初始化:使用字符串进行初始化

  1. ostringstream ostr("1234");  
  2. ostr.str("1234");//把字符串"1234"存入字符串流中  

举例:

  1. #include <iostream>   
  2. #include <sstream>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     //初始化输出字符串流ostr   
  7.     ostringstream ostr("1234");  
  8.     cout<<ostr.str()<<endl;//输出1234   
  9.       
  10.     ostr.put('5');//字符4顶替了1的位置   
  11.     cout<<ostr.str()<<endl;//输出5234   
  12.   
  13.     ostr<<"67";//字符串67替代了23的位置,输出5674   
  14.     string str = ostr.str();  
  15.     cout<<str<<endl;  
  16.     system("pause");  
  17.     return 1;  
  18. }  


原创粉丝点击