c++ stringstream(老好用了)

简介:

v前言:

    以前没有接触过stringstream这个类的时候,常用的字符串和数字转换函数就是sscanf和sprintf函数。开始的时候就觉得这两个函数应经很叼了,但是毕竟是属于c的。c++中引入了流的概念,通过流来实现字符串和数字的转换方便多了。在这里,总结之前的,并介绍新学的。

v常见格式串:  

  %% 印出百分比符号,不转换。
%c 整数转成对应的 ASCII 字元。
%d 整数转成十进位。
%f 倍精确度数字转成浮点数。
%o 整数转成八进位。
%s 整数转成字符串。
%x 整数转成小写十六进位。
%X 整数转成大写十六进位。
%n sscanf(str, "%d%n", &dig, &n),%n表示一共转换了多少位的字符

vsprintf函数

   sprintf函数原型为 int sprintf(char *str, const char *format, ...)。作用是格式化字符串,具体功能如下所示:

  (1)将数字变量转换为字符串。

  (2)得到整型变量的16进制和8进制字符串。

  (3)连接多个字符串。

复制代码
int main(){
    char str[256] = { 0 };
    int data = 1024;
    //将data转换为字符串
    sprintf(str,"%d",data);
    //获取data的十六进制
    sprintf(str,"0x%X",data);
    //获取data的八进制
    sprintf(str,"0%o",data);
    const char *s1 = "Hello";
    const char *s2 = "World";
    //连接字符串s1和s2
    sprintf(str,"%s %s",s1,s2);
    cout<<str<<endl; 
    return 0;
} 
复制代码

vsscanf函数

  sscanf函数原型为int sscanf(const char *str, const char *format, ...)。将参数str的字符串根据参数format字符串来转换并格式化数据,转换后的结果存于对应的参数内。具体功能如下:

  (1)根据格式从字符串中提取数据。如从字符串中取出整数、浮点数和字符串等。

  (2)取指定长度的字符串

  (3)取到指定字符为止的字符串

  (4)取仅包含指定字符集的字符串

  (5)取到指定字符集为止的字符串

  当然,sscanf可以支持格式串"%[]"形式的,有兴趣的可以研究一下。

复制代码
int main(){
    char s[15] = "123.432,432";
    int n;
    double f1;
    int f2;
    sscanf(s, "%lf,%d%n", &f1, &f2, &n);
    cout<<f1<<" "<<f2<<" "<<n;
    return 0;
} 
复制代码

  输出结果:123.432 432 11, 即一共转换了11位的字符。

vstringstream类:

  <sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。

  1.stringstream::str(); returns a string object with a copy of the current contents of the stream.

  2.stringstream::str (const string& s); sets s as the contents of the stream, discarding any previous contents.

  3.stringstream清空,stringstream s; s.str("");

  4.实现任意类型的转换

    template<typename out_type, typename in_value>
    out_type convert(const in_value & t){
      stringstream stream;
      stream<<t;//向流中传值
      out_type result;//这里存储转换结果
      stream>>result;//向result中写入值
      return result;
    }

复制代码
int main(){
    string s = "1 23 # 4";
    stringstream ss;
    ss<<s;
    while(ss>>s){
        cout<<s<<endl;
        int val = convert<int>(s);
        cout<<val<<endl;
    }
    return 0;
}
复制代码

  输出:1 1 23 23 # 0 4 4

  

  顺便说一下,今天做题的时候也用到了stringstream这个类,是二叉树的序列化和反序列化。

  题目链接:http://www.lintcode.com/zh-cn/problem/binary-tree-serialization/

v二叉树的序列化和反序列化

  设计一个算法,并编写代码来序列化和反序列化二叉树。将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”。如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉树序列化为一个字符串,并且可以将字符串反序列化为原来的树结构。

v思路:

  通过先序遍历建立二叉树的序列化,其中空子树用'#'来表示。反序列化的时候呢,遇到'#'就停止递归构造。另外序列化的时候是将整数通过stringstream转换成字符串,反序列化是将字符串通过stringstream转换成整数。

复制代码
/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * This method will be invoked first, you should design your own algorithm 
     * to serialize a binary tree which denote by a root node to a string which
     * can be easily deserialized by your own "deserialize" method later.
     */
    bool first;
    
    template<typename out_type, typename in_value>
    out_type convert(const in_value & t){
        stringstream stream;
        stream<<t;//向流中传值
        out_type result;//这里存储转换结果
        stream>>result;//向result中写入值
        return result;
    }
    
    void pre_order(TreeNode *root, string &s){
        if(root){
            string tmp = convert<string>(root->val);
            if(!first)
                s+= " "+tmp;
            else {
                first = false;
                s+=tmp;
            }
            pre_order(root->left, s);
            pre_order(root->right, s);
        } else {
            if(first)
                s+='#';
            else {
                first = false;
                s+=" #";
            }
        }
    }
    string serialize(TreeNode *root) {
        // write your code here
        string s="";
        first = true;
        pre_order(root, s);//先序实现序列化
        return s;
    }
    
    stringstream ss;
    void buildT(TreeNode * &T){
        string s;
        ss>>s;
        if(s == "#") return ;
        int val = convert<int>(s);
        T = new TreeNode(val);
        buildT(T->left);
        buildT(T->right);
    }
    
    /**
     * This method will be invoked second, the argument data is what exactly
     * you serialized at method "serialize", that means the data is not given by
     * system, it's given by your own serialize method. So the format of data is
     * designed by yourself, and deserialize it here as you serialize it in 
     * "serialize" method.
     */
    TreeNode *deserialize(string data) {
        // write your code here
        TreeNode *T = NULL;
        ss.str("");
        ss<<data;
        buildT(T);
        return T;
    }
};









本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/5042068.html,如需转载请自行联系原作者
目录
相关文章
|
3天前
|
编译器 C++
C++ 类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
45 30
|
17天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
1月前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)
|
17天前
|
C++
C++(十六)类之间转化
在C++中,类之间的转换可以通过转换构造函数和操作符函数实现。转换构造函数是一种单参数构造函数,用于将其他类型转换为本类类型。为了防止不必要的隐式转换,可以使用`explicit`关键字来禁止这种自动转换。此外,还可以通过定义`operator`函数来进行类型转换,该函数无参数且无返回值。下面展示了如何使用这两种方式实现自定义类型的相互转换,并通过示例代码说明了`explicit`关键字的作用。
|
17天前
|
存储 设计模式 编译器
C++(十三) 类的扩展
本文详细介绍了C++中类的各种扩展特性,包括类成员存储、`sizeof`操作符的应用、类成员函数的存储方式及其背后的`this`指针机制。此外,还探讨了`const`修饰符在成员变量和函数中的作用,以及如何通过`static`关键字实现类中的资源共享。文章还介绍了单例模式的设计思路,并讨论了指向类成员(数据成员和函数成员)的指针的使用方法。最后,还讲解了指向静态成员的指针的相关概念和应用示例。通过这些内容,帮助读者更好地理解和掌握C++面向对象编程的核心概念和技术细节。
|
30天前
|
存储 算法 编译器
c++--类(上)
c++--类(上)
|
1月前
|
编译器 C++
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
|
1月前
|
编译器 C++
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
|
1月前
|
编译器 C++
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
|
1月前
|
编译器 C++
【C++】类和对象(中)
【C++】类和对象(中)