zoukankan      html  css  js  c++  java
  • string常用函数的整理

    #include<bits/stdc++.h>
    using namespace std;
    
    template <class T>  
    bool scanff(T &ret){ //Faster Input  
         char c; int sgn; T bit=0.1;  
        if(c=getchar(),c==EOF) return 0;  
        while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();  
        sgn=(c=='-')?-1:1;  
        ret=(c=='-')?0:(c-'0');  
        while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');  
        if(c==' '||c=='
    '){ ret*=sgn; return 1; }  
        while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;  
        ret*=sgn;  
        return 1;  
    }  
    
    void run(string s){
        int pos = s.find('.');
        if(pos == -1) s.erase(pos, 1);
        else pos = s.size();
        int pos2 = 0;
        for(int i = 0; i < s.size(); ++i){
            if(s[i] > '0'){
                pos2 = i;
                break;
            }
        }
        int e = pos - pos2 - 1;
        s.erase(0, pos2);
    }
    
    void test(){
        string s;
        cin >> s;
        cout << "size: " << s.size() << endl;
        cout << "capacity: " << s.capacity() << endl;
        cout << "max_size: " << s.max_size() << endl;
        if(!s.empty())  cout << "not empty: " << s << endl;
    
        string t;// 赋值string 类型的 字符串
        cin >> t;
        cout << "s = t: " << (s = t) << endl;
        cout << "s.assign(t): " << (s.assign(t)) << endl;
    
        char str[100];//赋值 char 类型的字符串
        cin >> str;
        cout << "s.assign(str, 5): " << (s.assign(str, 5)) << endl;//赋值到5个字符
        cout << "s.assign(str, 1, 5): " << (s.assign(str, 1, 5)) << endl; //从1 开始的 5个字符
    }
    
    void test2(){
        string s, t;
        cin >> s;
        t = s.substr(2, 3); //返回pos开始的n个字符组成的字符串
        cout << t << endl;
        t = s;
        s.erase(2, 3); //删除[first,last)之间的所有字符,返回删除后迭代器的位置
        t = t.erase(2, 3);
        cout << t << endl;
    }
    
    int main(){
        ios::sync_with_stdio(false);
        //test2();
        //test();
        string str;
        cin >> str;
        cout << str << endl;
       // run(str);
        return 0;
    }
  • 相关阅读:
    Centos7安装Python3的方法
    word2vec原理(二) 基于Hierarchical Softmax的模型
    word2vec原理(一) CBOW与Skip-Gram模型基础
    爬虫的危害有多大
    python线程池实现
    进程和线程、协程的区别
    程序的编译与解释之间的区别
    【python3】如何建立爬虫代理ip池
    可能是史上最全的机器学习和Python(包括数学)速查表
    python 元类
  • 原文地址:https://www.cnblogs.com/boson-is-god/p/5777066.html
Copyright © 2011-2022 走看看