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;
}
原文地址:https://www.cnblogs.com/boson-is-god/p/5777066.html