zoukankan      html  css  js  c++  java
  • C++中的string 和 stringstream 的知识

    PS:最近用到了 stringstream 这个东西,于是顺便将 string 和 stringstream的一些知识点给看了看。 

    参考: https://blog.csdn.net/shs1992shs/article/details/83051298?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-5

    1.String 

    a. string 的拷贝

    string str = "Hello World!";
    const char* p1 = str.c_str();
    const char* p2 = str.data();
    const char* p3=new char[10];
    str.copy(p3,5,0);
    //函数原型:copy(char *s, int n, int pos = 0)
    //把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目
    

    b. string 中字符的查找与索引字符串

    string str = "aaaaddddssdfsasdf";  
    size_t pos = str.find("ssdf", 3);//注意pos的数据类型string::size_type
    //如果没找到,返回一个特殊的标志npos
    // 可以用if(pos != string::npos)则表示找到。
    string str2 = str.substr(pos, 5);
    

    2.stringstream 

    a.. stringstream 用于c++字符串的输入输出

    #include <iostream>
    #include <sstream>
    #include <string>
    #include<cstdlib>
    using namespace std;
    int main()
    {
            stringstream ostr("ccc");
            cout << ostr.str() << endl;
            ostr.put('d');
            ostr.put('e');
            cout << ostr.str() << endl;
            ostr << "fg";
            string gstr = ostr.str();
            cout << gstr << endl;
            char a;
            while (ostr >> a) {
                   cout << a << endl;
            }//可以看到stringstream对象既可以做输入流也可以做输出流
            system("pause");
    }
    输出:
    ccc
    dec
    defg
    d
    e
    f
    g
    

    b.stringstream用于数据类型转换

    #include <iostream>
    #include <sstream>
    #include <string>
    #include<cstdlib>
    using namespace std;
    
    int main()
    {
        stringstream sstr;
        //--------int转string-----------
        int a=100;
        string str;
        sstr<<a;
        sstr>>str;
        cout<<str<<endl;
        //--------string转char[]--------
        sstr.clear();//如果你想通过使用同一stringstream对象实现多种类型的转换,请注意在每一次转换之后都必须调用clear()成员函数。
        string name = "colinguan";
        char cname[200];
        sstr<<name;
        sstr>>cname;
        cout<<cname;
        system("pause");
    }
    

    c.stringstream用于空格分割的字符串的切分(这里直接举例子) 

    LeetCode:557. 反转字符串中的单词 
    给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
    示例 1:
    输入: "Let's take LeetCode contest"
    输出: "s'teL ekat edoCteeL tsetnoc"
    
    class Solution {
    public:
        string reverseWords(string s) {
            //使用C++的stringstream类和,string的reverse函数
            if(s.size() == 0)
                return "";
            stringstream ss;
            ss << s;
            string p,str;
            while(ss >> p){
                reverse(p.begin(), p.end()); //string 可以调用reverse函数,string的表现像一个vecor<char>
                str += p + ' ';
            }
            string ans(str.begin(), str.begin()+str.size()-1);
            return ans;
        }
    };
    

     

    我用到 stringstream的题目: 将输入的字符串指定单词替换为其他单词

    例如: "I will find u" 将 'u' 换成 ‘h’ ->  ”I will find h”

    #include<iostream>
    #inlucde<sstream>
    #include <cstring> 
    using namespace std; 
    
    int main()
    {
          string s, a ,b ;
          getline (cin,s); 
          cin >> a >> b;    
          stringsream ss = stringstream(s); 
          while(ss >> s ){
               if (s == 'a' )  cout << b << " ";
               else  cout << s << " "; 
        }
    }    
    

      

     

     

  • 相关阅读:
    [tip]build x86+x64 parrelly for your VS solution
    float double的内存表示及比较大小的方法
    [Problem 13]欧拉
    Interface Project
    [复习]内存对齐
    [tip]VS online Gallery in Extention Manager
    [Problem 14]欧拉
    “火柴棍式”程序员面试题打破惯性思维
    [复习]时间复杂度及计算
    ModuleCatalog配置文件
  • 原文地址:https://www.cnblogs.com/E-Dreamer-Blogs/p/12855361.html
Copyright © 2011-2022 走看看