string学习

#include <iostream>
#include <string>
using namespace std;
int main()
{
    cout<<"============string基本的赋值==============="<<endl;
    string str;
    string str2(str);
    string str3("value");
    string str4(10,'c');
    cout<<str<<endl;
    cout<<str2<<endl;
    cout<<str3<<endl;
    cout<<str4<<endl;
    cout<<str3+","+str4<<endl;
    cout<<"============string对象获取字符==============="<<endl;
    string strTemp("some people"); 
    cout<<"============string::size_type,strTemp.length()==============="<<endl;
    for (string::size_type it=0;it!=strTemp.length();++it)
    {
        cout<<strTemp[it]<<endl;
    }
    cout<<"============iterator,strTemp.begin(),strTemp.end()==============="<<endl;
    for (string::iterator ri=strTemp.begin();ri!=strTemp.end();++ri)
    {
        cout<<*ri<<endl;}cout<<"============string::size_type,string::npos==============="<<endl;
        //这种方法不行,因为stringn::npos可能不是一般++ri1能达到的
        // for (string::size_type ri1=0;ri1!=string::npos/*strTemp.size()*/;++ri1)
        // {
        // cout<<strTemp[ri1]<<endl;
        // }
        for (string::iterator ri=strTemp.begin();ri!=strTemp.end();++ri)
        {
            *ri=toupper(*ri);cout<<*ri<<endl;
        }
        string::size_type i;i=strTemp.find('E');
        if (i==string::npos)
        {
            cout<<"未找到"<<endl;
        }
        else
        {
            cout<<"找到了"<<i<<endl;
        }
        return 0;
}

 比较好的介绍:http://www.cnblogs.com/gaojun/archive/2010/09/11/1824016.html

posted @ 2013-08-20 22:21  cheshulin  阅读(158)  评论(0编辑  收藏  举报