• 【转】string类使用示例(源代码)


    http://blog.sina.com.cn/s/blog_51409e8f01009h7k.html~type=v5_one&label=rela_nextarticle

    1)
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s("hehe");
        cout<<s<<endl;
        cin.get();
    }
    2)
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        char chs[] = "hehe";
        string s(chs);
        cout<<s<<endl;
        cin.get();
    }
    3)
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        char chs[] = "hehe";
        string s(chs,1,3);    //指定从chs的索引1开始,最后复制3个字节
        cout<<s<<endl;
        cin.get();
    }
    4)
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s1("hehe");
        string s2(s1);   
        cout<<s2<<endl;
        cin.get();
    }
    5)
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s1("hehe",2,3);
        string s2(s1);   
        cout<<s2<<endl;
        cin.get();
    }
    6)
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        char chs[] = "hehe";
        string s(chs,3);    //将chs前3个字符作为初值构造
        cout<<s<<endl;
        cin.get();
    }
    7)
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s(10,'k');    //分配10个字符,初值都是'k'
        cout<<s<<endl;
        cin.get();
    }
    //以上是string类实例的构造手段,都很简单.

    9)
    //赋新值
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s(10,'k');    //分配10个字符,初值都是'k'
        cout<<s<<endl;
        s = "hehehehe";
        cout<<s<<endl;
        s.assign("kdje");
        cout<<s<<endl;
        s.assign("fkdhfkdfd",5);    //重新分配指定字符串的前5的元素内容
        cout<<s<<endl;       
        cin.get();
    }
    10)
    //swap方法交换
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s1 = "hehe";
        string s2 = "gagaga";
        cout<<"s1 : "<<s1<<endl;
        cout<<"s2 : "<<s2<<endl;
        s1.swap(s2);
        cout<<"s1 : "<<s1<<endl;
        cout<<"s2 : "<<s2<<endl;
        cin.get();
    }
    11)
    //+=,append(),push_back()在尾部添加字符
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "hehe";
        s += "gaga";
        cout<<s<<endl;
        s.append("嘿嘿");    //append()方法可以添加字符串
        cout<<s<<endl;
        s.push_back('k');    //push_back()方法只能添加一个字符...
        cout<<s<<endl;
        cin.get();
    }
    12)
    //insert() 插入字符.其实,insert运用好,与其他的插入操作是一样的.
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "hehe";
        s.insert(0,"头部");            //在头部插入
        s.insert(s.size(),"尾部");    //在尾部插入
        s.insert(s.size()/2,"中间");//在中间插入
        cout<<s<<endl;
        cin.get();
    }
    13)
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "abcdefg";
        s.erase(0,1);    //从索引0到索引1,即删除掉了'a'
        cout<<s<<endl;
        //其实,还可以使用replace方法来执行删除操作
        s.replace(2,3,"");//即将指定范围内的字符替换成"",即变相删除了
        cout<<s<<endl;
        cin.get();
    }

    14)
    //clear() 删除全部字符
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "abcdefg";
        cout<<s.length()<<endl;
        s.clear();
        cout<<s.length()<<endl;
        //使用earse方法变相全删除
        s = "dkjfd";
        cout<<s.length()<<endl;
        s.erase(0,s.length());
        cout<<s.length()<<endl;

        cin.get();
    }
    15)
    //replace() 替换字符
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "abcdefg";
        s.replace(2,3,"!!!!!");//从索引2开始3个字节的字符全替换成"!!!!!"
        cout<<s<<endl;
        cin.get();
    }
    16)
    //==,!=,<,<=,>,>=,compare()  比较字符串
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s1 = "abcdefg";
        string s2 = "abcdefg";   
        if (s1==s2)cout<<"s1 == s2"<<endl;
        else cout<<"s1 != s2"<<endl;
       
        if (s1!=s2)cout<<"s1 != s2"<<endl;
        else cout<<"s1 == s2"<<endl;
       
        if (s1>s2)cout<<"s1 > s2"<<endl;
        else cout<<"s1 <= s2"<<endl;
       
        if (s1<=s2)cout<<"s1 <= s2"<<endl;
        else cout<<"s1 > s2"<<endl;

        cin.get();
    }
    17)
    //size(),length()  返回字符数量
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "abcdefg";
        cout<<s.size()<<endl;
        cout<<s.length()<<endl;

        cin.get();
    }
    18)
    //max_size() 返回字符的可能最大个数
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "abcdefg";
        cout<<s.max_size()<<endl;

        cin.get();
    }
    19)
    //empty()  判断字符串是否为空
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s ;
        if (s.empty())
            cout<<"s 为空."<<endl;
        else
            cout<<"s 不为空."<<endl;

        s = s + "abcdefg";
        if (s.empty())
            cout<<"s 为空."<<endl;
        else
            cout<<"s 不为空."<<endl;

        cin.get();
    }
    20)
    // [ ], at() 存取单一字符
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "abcdefg1111";
       
        cout<<"use []:"<<endl;
        for(int i=0; i<s.length(); i++)
        {
            cout<<s[i]<<endl;
        }
        cout<<endl;

        cout<<"use at():"<<endl;
        for(int i=0; i<s.length(); i++)
        {
            cout<<s.at(i)<<endl;
        }
        cout<<endl;
       
        cin.get();
    }
    21)
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "abcdefg1111";
       
        const char * chs1 = s.c_str();
        const char * chs2 = s.data();

        cout<<"use at():"<<endl;
        int i;
        for(i=0; i<s.length(); i++)
        {
            cout<<"c_str() : "<<chs1[i]<<endl;
            cout<<"data() : "<<chs2[i]<<endl;
        }
        cout<<"c_str() : "<<chs1<<endl;
        cout<<"data() : "<<chs2<<endl;
        cout<<endl;
       
        cin.get();
    }
    22)
    // substr() 返回某个子字符串
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "abcdefg1111";
       
        string str = s.substr(5,3);//从索引5开始3个字节
        cout<<str<<endl;
       
        cin.get();
    }
    23)
    // find 查找函数
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "abcdefg1111";
        string pattern = "fg";
        string::size_type pos;
        pos = s.find(pattern,0);        //从索引0开始,查找符合字符串"f"的头索引
        cout<<pos<<endl;
        string str = s.substr(pos,pattern.size());
        cout<<str<<endl;
        cin.get();
    }
    24)
    // begin() end() 提供类似STL的迭代器支持
    #include <string>
    #include <iostream>
    using namespace std;

    void main()
    {
        string s = "abcdefg1111";
        for(string::iterator iter = s.begin(); iter!=s.end(); iter++)
        {
            cout<<*iter<<endl;
        }
        cout<<endl;

        cin.get();
    }

     

  • 相关阅读:
    Atitit 趋势管理之道 attilax著
    Atitit 循环处理的新特性 for...else...
    Atitit 2017年的技术趋势与未来的大技术趋势
    atitit 用什么样的维度看问题.docx 如何了解 看待xxx
    atitit prj mnrs 项目中的几种经理角色.docx
    Atitit IT办公场所以及度假村以及网点以及租房点建设之道 attilax总结
    Atitit 工具选型的因素与方法 attilax总结
    Atitit.团队文化建设影响组织的的一些原理 法则 定理 效应 p826.v4
    Atiitt 管理方面的误区总结 attilax总结
    Atitit 未来趋势把控的书籍 attilax总结 v3
  • 原文地址:https://www.cnblogs.com/fzzl/p/1521765.html
Copyright © 2020-2023  润新知