构造

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

int main()
{
    string s1="helloworld";
    string s2;
    string s3(s1);
    string s4(s1,3,2);//从3开始输出2个
    string s5(s1,2);//从2开始一直到最后
    string s6(10,'h');
    cout << "s1:"<<s1<< endl;
    cout << "s2:"<<s2<< endl;
    cout << "s3:"<<s3<< endl;
    cout << "s4:"<<s4<< endl;
    cout << "s5:"<<s5<< endl;
    cout << "s6:"<<s6<< endl;
    return 0;
}

访问

    cout << "s1:"<<s1<< endl;
    for(int i=0;i<s1.size();i++){
        cout<<"the "<<i+1<<" th char is "<<s1[i]<<endl;

插入删除 

插入

string n="012345678",n1,n2;
    cout << "n:"<<n<< endl;
    n.insert(6,"insert");//第六个元素变成i
    cout << "n:"<<n<< endl;
    n.insert(3,"insert",1,2);//第34个元素变成插入字符串的第[1,2]个元素
    cout << "n:"<<n<< endl;
    return 0;

 

 删除

    string n="012345678",n1,n2;
    cout << "n:"<<n<< endl;
    n.erase(3,2);//从3开始删,删2个
    cout << "n:"<<n<< endl;

    n="012345678",n1,n2;
    cout << "n:"<<n<< endl;
    n.erase(3);//从3开始删,删2个
    cout << "n:"<<n<< endl;//从3开始删,删到最后

 

n.clear();
cout << "n:"<<n<< endl;

运算 

    string a="abc";
    string b="ab";
    string x="abd";
    string c=a+b;
    cout << (a>b)<< endl;//true长的比短的大
    cout <<(a>x)<<endl;//false后的比前的大
    cout << c<<endl;

函数 

    string s="cxoocxoopcx";
    cout <<s<< endl;
    cout <<s.size()<< endl;

    cout<<s.find("cx")<<endl;
    cout<<s.find("cx",4)<<endl;//4
    cout<<s.find("kk")<<endl;//不存在:返回一个混乱的大数

    cout<<s.substr(4)<<endl;//从4到最后
    cout<<s.substr(4,2)<<endl;//从4开始截取两个