zoukankan      html  css  js  c++  java
  • 有关于string的一些用法

    string s;
    s.erase(2,4)删除 下标为2,长度为4的字符串
    s.empty()判断是否为空
    s.substr(0,5)子序列  从第0位开始的长度为5的字符串

    s.replace(0,2,"VK") 用"VK"代替从坐标0开始的连续2个的字符串
    s.find()查找  函数返回 在 str中第一次出现下标的位置
    s.length()计算序列长度
    cin.getline(s,sizeof(s));例如gets类的输入     string str;   getline(cin,str);
    sort(s.begin(),s.end())排序


    string 作为容器
    string str;
    str.push_back('q');压入
    string::iterator itstr = str.begin();
    for( ;itstr!=str.end; itstr++)
    cout<<*itstr;
    sort(str.begin(),str.end);排序

     


    int main()
    {
        ios_base::sync_with_stdio(0);cin.tie();
        用于cin,cout的加速
    }

     

    #include <iostream>
    #include <sstream>
    using namespace std;

    int main()
    {   /*
        *数字向字符转换
        */
        string s1;
        int s2;
        stringstream s3;
        cin >> s2;
        s3 << s2;
        s3 >> s1;
        cout << s1 <<endl;
       
       
       
        /*
        *字符向数字转换
        */
        string s1;
        int s2;
        stringstream s3;
        cin >> s1;
        s3 << s1;
        s3 >> s2;
        cout << s2 <<endl;
        return 0;
    }
    如果你打算在多次转换中使用同一个stringstream对象,记住再每次转换前要使用clear()方法;
    在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。

     

     

     


    #include <iostream>
    #include <sstream>
    using namespace std;//字符串中间有空格,一整行处理

    int main()
    {   stringstream ss;
        string s, a, b, c;
        getline(cin,s); == cin.getline(s,1000,' ');
        ss.str(s);//将指定字符串射成一开始内容
        ss >> a >> b >> c;
        cout << a << " " <<b  << " " << c << endl;
        return 0;
    }

     

    #include <iostream>
    #include <sstream>
    #include <cstring>
    #include <string>
    #include <cstdio>
    using namespace std;

    int main()
    {
        /*
        *string转换到char
        */
        /*string a;
        char b[100];
        cin >> a;
        strcpy(b,a.c_str());
        printf("%s",b);


        /*
        * char转换到string
        */
        string a;
        char b[100];
        scanf("%s",b);
        a = b;
        cout << a << endl;
        return 0;
    }

    string s1;

    int a = 6;

    string str = s1 + std::to_string(a);由数字转向字符

  • 相关阅读:
    [java][JEECG] Maven settings.xml JEECG项目初始化 RouYi settings.xml配置
    Deepin-TIM或Deepin-QQ调整界面DPI字体大小的方法
    deepin 深度Linux系统 15.11 链接蓝牙鼠标问题
    安装vs code之后,win+e快捷键打开的是vs code,而不是文件管理器,解决方法
    【golang】使用rpcx不指定tags报错 undefined: serverplugin.ConsulRegisterPlugin
    【Python】安装MySQLdb模块centos 6.1 宝塔Linux面板 MySQL5.6
    [Nginx]配置文件详解
    linux 下终端通过证书、私钥快捷登录
    npm 更新包
    golang 无缓冲channel
  • 原文地址:https://www.cnblogs.com/creativepower/p/6601659.html
Copyright © 2011-2022 走看看