C++--STL----string

string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是用char*表示的。string与char*都可以用来表示字符串,那么二者有什么区别呢。
string和char*的比较
string是一个类, char*是一个指向字符的指针。
         string封装了char*,管理这个字符串,是一个char*型的容器。
string不用考虑内存释放和越界。
         string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。
string提供了一系列的字符串操作函数(这个等下会详讲)

         查找find,拷贝copy,删除erase,替换replace,插入insert

string在STL中一般有十几种方式。

一,string的构造函数

        默认构造函数:
             string();    //构造一个空的字符串string s1。
        拷贝构造函数:
             string(const string &str);     //构造一个与str一样的string。如string s1(s2)。
         带参数的构造函数
            string(const char *s);    //  用字符串s初始化

            string(int n,char c);        //  用n个字符c初始化

void StringInit()   //字符串构造函数,4种方式
{
    string s1;
    string s2("helloworld");
    string s3(s2);
    string s4(10,'a');

    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
}

二,string的赋值

            string &operator=(const string &s);     //把字符串s赋给当前的字符串
            string &assign(const char *s);               //把字符串s赋给当前的字符串
            string &assign(const char *s, int n);    //把字符串s的前n个字符赋给当前的字符串
            string &assign(const string &s);           //把字符串s赋给当前字符串
            string &assign(int n,char c);                  //用n个字符c赋给当前字符串

            string &assign(const string &s,int start, int n);  //把字符串s中从start开始的n个字符赋给当前字符串

void StringAssign()  //给字符串赋值
{
    string s1("helloworld");
    string s2("test");
    char *s = "wangsheng";
    s2 = s1;
    cout << s2 << endl; 

    s2.assign(s);  //完全拷贝
    cout << s2 << endl;

    s2.assign(s,5); // 拷贝前五个
    cout << s2 << endl;

    s2.assign(s1);
    cout << s2 << endl;

    s2.assign(10,'a');  //赋值10 个’a‘
    cout << s2 << endl;

    s2.assign(s1,5,2);  //从s1的第五个字符往后复制2个给s2
    cout << s2 << endl;
}

三,string中的查找,替换

                查找
                    int find(char c,int pos=0) const;  //从pos开始查找字符c在当前字符串的位置 
                    int find(const char *s, int pos=0) const;  //从pos开始查找字符串s在当前字符串的位置
                    int find(const string &s, int pos=0) const;  //从pos开始查找字符串s在当前字符串中的位置
                    find函数如果查找不到,就返回-1
                    int rfind(char c, int pos=npos) const;   //从pos开始从后向前查找字符c在当前字符串中的位置 
                    int rfind(const char *s, int pos=npos) const;
                    int rfind(const string &s, int pos=npos) const;
                    //rfind是反向查找的意思,如果查找不到, 返回-1
                替换
                    string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s
                     string &replace(int pos, int n, const string &s);  //删除从pos开始的n个字符,然后在pos处插入串s

                    void swap(string &s2);    //交换当前字符串与s2的值

void StringFind()  //字符串查找,找到后返回字符串位置;可定义从什么位置开始寻找,可以替换
{
    string s1("helloworldhelloworld");
    string s2("world");

    int index = s1.find('h',1);
    cout << index << endl;

    index = s1.find("hello",0);
    cout << index << endl;

    index = s1.find(s2,0);
    cout << index << endl;

    index = s1.find("hello",0);
    while(-1 != index)
    {
        s1.replace(0,5,"xx");
        index = s1.find("hello",index + strlen("xx"));
    }
    cout << s1 << endl;
}

三,sting的拷贝

            int copy(char *s, int n, int pos=0) const;  

              把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目。注意要保证s所指向的空                   间足够大以容纳当前字符串,不然会越界。

void StringCopy()  //字符串拷贝
{
    string s1("helloworld");
    char buf[64] = {0};
    s1.copy(buf,5,2);
    cout << buf << endl;
}


四,string的删除之后插入

            string &insert(int pos, const char *s);
            string &insert(int pos, const string &s);
            //前两个函数在pos位置插入字符串s
            string &insert(int pos, int n, char c);  //在pos位置 插入n个字符c
            string &erase(int pos=0, int n=npos);  //删除pos开始的n个字符,返回修改后的字符串

void StringInsert()  //字符串删除插入,可定义插入的位置
{
    string s1("helloworld");
    string s2("test");
    s1.insert(5,"wang");
    cout << s1 << endl;

    s1.insert(7,s2);
    cout << s1 << endl;

    s2.insert(2,3,'c');
    cout << s2 << endl;

    s1.erase(5,5);
    cout << "s1: " << s1 << endl;

}

五,sting的查找替换

            查找
            int find(char c,int pos=0) const;  //从pos开始查找字符c在当前字符串的位置 
            int find(const char *s, int pos=0) const;  //从pos开始查找字符串s在当前字符串的位置
            int find(const string &s, int pos=0) const;  //从pos开始查找字符串s在当前字符串中的位置
            find函数如果查找不到,就返回-1
            int rfind(char c, int pos=npos) const;   //从pos开始从后向前查找字符c在当前字符串中的位置 
            int rfind(const char *s, int pos=npos) const;
            int rfind(const string &s, int pos=npos) const;
            //rfind是反向查找的意思,如果查找不到, 返回-1
        替换
                string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s
                string &replace(int pos, int n, const string &s);  //删除从pos开始的n个字符,然后在pos处插入串s
                void swap(string &s2);    //交换当前字符串与s2的值
 

void StringFind()  //字符串查找,找到后返回字符串位置;可定义从什么位置开始寻找
{
    string s1("helloworldhelloworld");
    string s2("world");

    int index = s1.find('h',1);
    cout << index << endl;

    index = s1.find("hello",0);
    cout << index << endl;

    index = s1.find(s2,0);
    cout << index << endl;

    index = s1.find("hello",0);
    while(-1 != index)
    {
        s1.replace(0,5,"xx");  //字符串替换
        index = s1.find("hello",index + strlen("xx"));
    }
    cout << s1 << endl;
}

六,string的取字符操作

            string类的字符操作:
            const char &operator[] (int n) const;
            const char &at(int n) const;
            char &operator[] (int n);
            char &at(int n);
            operator[]和at()均返回当前字符串中第n个字符,但二者是有区别的。
            主要区别在于at()在越界时会抛出异常,[]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。   

void StringAt()
{
    string s1("helloworld");
    //cout << s1[500000] << endl;  //越界访问,段错误
    try
    {
        cout << s1.at(15) << endl;         // 越界访问,抛出异常
    }
    catch(exception &e)
    {
        cout << "catch a exception:" << e.what() << endl;
    }
}

七,string的从string取得const char*的操作

           const char *c_str() const;   //返回一个以'\0'结尾的字符串的首地址

void StringStr()
{
    string s2("helloworld");
    const char* str;
    str = s2.c_str();    // 从str中取得const char*的操作
    cout << str << endl;

}

八,string的长度

int length() const;         //返回当前字符串的长度。长度不包括字符串结尾的'\0'。
bool empty() const;     //当前字符串是否为空
 

void StringLength()  //求字符串长度
{
    string s2("helloworld");
    cout << s2.length() << endl;
    if(s2.empty())
    {
        cout << "null" << endl;
    }
    else
    {
        cout << "not null" << endl;
    }
}

九,string的链接

        string &operator+=(const string &s);  //把字符串s连接到当前字符串结尾
        string &operator+=(const char *s);//把字符串s连接到当前字符串结尾
        string &append(const char *s);    //把字符串s连接到当前字符串结尾
        string &append(const char *s,int n);  //把字符串s的前n个字符连接到当前字符串结尾
        string &append(const string &s);   //同operator+=()
        string &append(const string &s,int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
        string &append(int n, char c);   //在当前字符串结尾添加n个字符c
 

 
void StringAppend()  //字符串链接
{
    string s1("helloworld");
    string s2("123456");
    char *s = "wangsheng";
     
    s1 = s1 + s2;
    s1 = s1 + s;
    cout << s1 << endl;

    s1.append(s);
    cout << s1 << endl;

    s1.append(s,5);
    cout << s1 << endl;

    s1.append(s2);
    cout << s1 << endl;

    s1.append(s2,3,4);
    cout << s1 << endl;

    s1.append(10,'a');
    cout << s1 << endl;

}

十,string的比较

            int compare(const string &s) const;  //与字符串s比较
            int compare(const char *s) const;   //与字符串s比较
            compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小大写的A比小写的a小。
 

void StringCompare()   // 字符串比较
{
    string s1("hello");
    string s2("wang");
    if(-1 == s1.compare(s2))
    {
        cout << "s1 < s2" << endl;
    }
    else if(1 == s1.compare(s2))
    {
        cout << "s1 > s2" << endl;
    }
    else
    {
        cout << "s1 = s2" << endl;
    }
}

十一,string的子串

            string substr(int pos=0, int n=npos) const;    //返回由pos开始的n个字符组成的子字符串
 

void StringSub()  //返回需要的字符串
{
    string s1("helloworldhelloworldhelloworld");
    string s2;
    s2 = s1.substr(5,4);  //把从s1的第五个字符开始往后复制器四个给s2
    cout << s2 << endl;
}
十二,string的算法相关
 
string s2 = "AAAbbb";
    transform(s2.begin(), s2.end(), s2.begin(), toupper);
    cout << s2 << endl;
 
    string s3 = "AAAbbb";
    transform(s3.begin(), s3.end(), s3.begin(), tolower);
    cout << s3 << endl;
这里总结了string的十二中相关。

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL是指标准模板库(Standard Template Library),它是C++语言的一部分,提供了一系列的模板类和函数,用于支持通用的数据结构和算法。STL的目标是提供高效、可重用和可扩展的组件,以便开发人员能够更轻松地编写高质量的代码。STL包含了许多常见的数据结构,如vector、list、set、map等,以及各种算法,比如排序、查找、遍历等。通过使用STL,开发人员可以更加高效地处理各种数据结构和算法的问题,提高代码的开发效率和质量。 在STL中,我们可以使用各种容器来存储和管理数据。例如,我们可以使用std::map来创建一个键值对的映射,其中每个键都有一个与之相关联的值。下面是一个示例代码,展示了如何创建和使用一个std::map对象: std::map<std::string, int> disMap() { std::map<std::string, int> tempMap{ {"C语言教程",10},{"STL教程",20} }; return tempMap; } std::map<std::string, int> newMap(disMap()); 在这个示例中,disMap()函数创建了一个临时的std::map对象,并初始化了其中的一些键值对。然后,使用移动构造函数将这个临时对象移动到了一个新的std::map对象newMap中。最终,我们可以通过newMap对象来访问和操作这些键值对。 综上所述,STLC++中的标准模板库,提供了一系列的模板类和函数,用于支持通用的数据结构和算法。STL的使用可以提高代码的开发效率和质量,并且通过各种容器和算法,可以方便地处理各种数据结构和算法的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++ STL详解超全总结(快速入门STL)](https://blog.csdn.net/qq_50285142/article/details/114026148)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【C++实验】阅读STL源码并分析](https://blog.csdn.net/qq_35760825/article/details/125311509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值