zoukankan      html  css  js  c++  java
  • STL string

    初始化:

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        string s;//默认初始化,一个空字符串
        string s1("ssss");//s1是字面值“ssss”的副本
        string s2(s1);//s2是s1的副本
        string s3=s2;//s3是s2的副本
        string s4(10,'c');//把s4初始化
        string s5="hiya";//拷贝初始化
    }

     

    字符串处理:

    substr操作:

    注意substr没有迭代器作为参数的操作

    1 //s.substr(pos1,n)返回字符串位置为pos1后面的n个字符组成的串
    2     string s2=s.substr(1,5);//abcde
    3 
    4     //s.substr(pos)//得到一个pos到结尾的串
    5     string s3=s.substr(4);//efg

    insert操作:

     1     string str="to be question";
     2     string str2="the ";
     3     string str3="or not to be";
     4     string::iterator it;
     5 
     6     //s.insert(pos,str)//在s的pos位置插入str
     7     str.insert(6,str2);                 // to be the question
     8 
     9     //s.insert(pos,str,a,n)在s的pos位置插入str中插入位置a到后面的n个字符
    10     str.insert(6,str3,3,4);             // to be not the question
    11 
    12     //s.insert(pos,cstr,n)//在pos位置插入cstr字符串从开始到后面的n个字符
    13     str.insert(10,"that is cool",8);    // to be not that is the question
    14 
    15     //s.insert(pos,cstr)在s的pos位置插入cstr
    16     str.insert(10,"to be ");            // to be not to be that is the question
    17 
    18     //s.insert(pos,n,ch)在s.pos位置上面插入n个ch
    19     str.insert(15,1,':');               // to be not to be: that is the question

    erase操作:

    用来执行删除操作
    删除操作有三种

    指定pos和len,其中pos为为起始位置,pos以及后面len-1个字符串都删除

    迭代器,删除迭代器指向的字符

    迭代器范围,删除这一范围的字符串,范围左闭右开

     1 std::string str ("This is an example sentence.");
     2   std::cout << str << '
    ';
     3                           // "This is an example sentence."
     4   str.erase (10,8);       //            ^^^^^^^^
     5   //直接指定删除的字符串位置第十个后面的8个字符
     6   std::cout << str << '
    ';
     7                             // "This is an sentence."
     8   str.erase (str.begin()+9);//           ^
     9   //删除迭代器指向的字符
    10   std::cout << str << '
    ';
    11                             // "This is a sentence."
    12                             //       ^^^^^
    13   str.erase (str.begin()+5, str.end()-9);
    14   //删除迭代器范围的字符
    15   std::cout << str << '
    ';
    16                             // "This sentence."

     

    string的搜索操作:

    find和rfind函数:

    find函数主要是查找一个字符串是否在调用的字符串中出现过,大小写敏感。

    1 //在str当中查找第一个出现的str2,找到则返回出现的位置,否则返回结尾
    2     std::size_t found = str.find(str2);
    3 
    4  //在str当中,从第found+1的位置开始查找参数字符串的前6个字符
    5     found=str.find("needles are small",found+1,6);
    6 
    7 //在str当中查找参数中的字符串
    8     found=str.find("haystack");

    rfind函数就是找最后一个出现的匹配字符串,返回的位置仍然是从前往后数的。

    1   std::string str ("The sixth sick sheik's sixth sheep's sick.");
    2     std::string key ("sixth");//                    ^
    3     //rfind是找最后一个出现的匹配字符串
    4     std::size_t found = str.rfind(key);

    compare函数:

    和strcmp函数一样,如果两个字符串相等,那么返回0,调用对象大于参数返回1,小于返回-1。

    1 string s1="123",s2="123";
    2     cout<<s1.compare(s2)<<endl;//0
    3 
    4     s1="123",s2="1234";
    5     cout<<s1.compare(s2)<<endl;//-1
    6 
    7     s1="1234",s2="123";
    8     cout<<s1.compare(s2)<<endl;//1
  • 相关阅读:
    golang之panic,recover,defer
    Golang之函数练习
    Golang之strings包
    Golang之字符串操作(反转中英文字符串)
    keil中使用——变参数宏__VA_ARGS__
    到底该不该用RTOS——rtos的优点
    c语言联合union的使用用途
    c语言的#和##的用法
    c语言位域的使用注意事项——数据溢出
    基于 Keil MDK 移植 RT-Thread Nano
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/7799444.html
Copyright © 2011-2022 走看看