zoukankan      html  css  js  c++  java
  • C++字符串类string简析

    字符串操作是一个不小的主题,在标准C++中,string字符串类成为一个标准,之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下的需要.
        下面我们首先从一些示例开始学习下string类的使用.
    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();
    }
            一个C++字符串存在三种大小:a)现有的字符数,函数是size()和length(),他们等效。 Empty()用来检查字符串是否为空。b)max_size() 这个大小是指当前C++字符串最多能包含的字符数,很可能和机器本身的限制或者字符串所在位置连续内存的大小有关系。我们一般情况下不用关心他,应该大小足够我们用的。但是不够用的话,会抛出length_error异常c)capacity()重新分配内存之前 string所能包含的最大字符数。这里另一个需要指出的是reserve()函数,这个函数为string重新分配内存。重新分配的大小由其参数决定,默认参数为0,这时候会对string进行非强制性缩减
     
  • 相关阅读:
    jquery选择器中逗号的使用
    超大文件上传和断点续传的代码
    超大文件上传和断点续传的源代码
    超大文件上传和断点续传的源码
    超大文件上传和断点续传的控件
    超大文件上传和断点续传的插件
    杨辉三角
    pkg-config的用法
    c++类的实例化,有没有new的区别
    正则表达式中元字符
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9410221.html
Copyright © 2011-2022 走看看