#include<string.h>

#include<string.h>//头文件
#include<stdio.h>
#include <vector>
#include <iostream>
using namespace std;

void main_1()
{
    string s;//创建
    cout<<s.length()<<endl;//返回长度
    char tmp[3]={'a','b','c'};
    s.append(tmp,3);//尾部添加
    cout<<s.c_str()<<endl;
    s="abcdefg";
    cout<<s.c_str()<<endl;
    cout<<"--------------------"<<endl;

    string::iterator it=s.begin();
    s.erase(it+3);//删除第三个
    cout<<s.c_str()<<endl;
    s.erase(it,it+4);//删除前4个
    cout<<s.c_str()<<endl;
    s="";//清空 s.clear();//清空
    cout<<s.c_str()<<endl;
    cout<<"--------------------"<<endl;
    return;
}
/*
0
abc
abcdefg
--------------------
abcefg
fg

--------------------
请按任意键继续. . .
*/

void main_2()
{
    string s("abcdefg");
    cout<<s.empty()<<endl;;//空返回1,否则返回0
    s.replace(3,3,"xx");//第3个开始的三个用xx替换
    cout<<s.c_str()<<endl;
    cout<<"--------------------"<<endl;
}
/*
0
abcxxg
--------------------
请按任意键继续. . .
*/

void main_3()
{
    string s("abcdefg");
    int index=s.find('c');//查找 返回下标
    cout<<index<<endl;
    index=s.find("abc");
    cout<<index<<endl;
    index=s.find("cba");//找不到返回-1
    cout<<index<<endl;
    //
    cout<<s.compare("abcd")<<endl; //比较 比对方大返回1 相等返回0 小返回-1
    reverse(s.begin(),s.end());//反向排序
    cout<<s.c_str()<<endl;
    return;
}
/*
2
0
-1
1
gfedcba
请按任意键继续. . .
*/

ostream& operator << (ostream & out, const string &str)
{
    out<<str.c_str();
    return out;
}

void main()
{
    
    vector< string >v;//近似于二位数组的用法
    v.push_back("abc");
    v.push_back("def");
    v.push_back("gh");
    cout<<v[1]<<endl;
    cout<<v[1][2]<<endl;
    cout<<v[2]<<endl;
    cout<<v[2].length()<<endl;    
}
/*
def
f
gh
2
请按任意键继续. . .
*/

 

posted @ 2018-03-02 10:42  sky20080101  阅读(254)  评论(0)    收藏  举报