zoukankan      html  css  js  c++  java
  • C++中的String的常用函数用法总结

    1. string的构造函数

    string str1;               //生成空字符串
    string str2("123456789");  //生成"1234456789"的复制品
    string str3("12345", 0, 3);//结果为"123"
    string str4("012345", 5);  //结果为"01234"
    string str5(5, '1');       //结果为"11111"
    string str6(str2, 2);      //结果为"3456789"
    
        
    

    2. string的大小

    size()和length():返回string对象的字符个数,他们执行效果相同。
    

    3. string的插入:push_back() 和 insert()

     // 尾插一个字符
    s1.push_back('a');
    s1.push_back('b');
    s1.push_back('c');
    cout<<"s1:"<<s1<<endl;    // s1:abc
    
    // insert(pos,char):在制定的位置pos前插入字符char
    s1.insert(s1.begin(),'1');
    cout<<"s1:"<<s1<<endl;   // s1:1abc
    

    4. string的返回:front() 和 back()

    string a="abcd";
    
    1.获取字符串最后一个字符
    auto b=a.back(); //结果为 b='d';
    2.修改字符串最后一个字符
    a.back()='!'; //结果为 a="abc!";
    
    3.获取字符串第一个字符
    auto b=a.front(); //结果为 b='a';
    4.修改字符串第一个字符
    a.front()='!'; //结果为 a="!bcd";
    

    5. string拼接字符串:append() 、+ 操作符

     // 方法一:append()
        string s1("abc");
        s1.append("def");
        cout<<"s1:"<<s1<<endl;     // s1:abcdef
    
     // 方法二:+ 操作符
        string s2 = "abc";
        /*s2 += "def";*/
        string s3 = "def";
        s2 += s3;
        cout<<"s2:"<<s2<<endl;     // s2:abcdef
    

    6.  string的遍历:迭代器或者下标法

     string s1("abcdef"); // 调用一次构造函数
    
    // 方法一: 下标法
        for( int i = 0; i < s1.size() ; i++ )
        {
            cout<<s1[i];
        }
    
    // 方法二:正向迭代器
        string::iterator iter = s1.begin();
        for( ; iter < s1.end() ; iter++)
        {
            cout<<*iter;
        }
    
    // 方法三:反向迭代器
        string::reverse_iterator riter = s1.rbegin();
        for( ; riter < s1.rend() ; riter++)
        {
            cout<<*riter;
        }
        cout<<endl;
    

    7.  string的删除:erase()

    (1). basic_string & erase(size_type pos=0, size_type n=npos);
    从给定起始位置pos处开始删除, 要删除字符的长度为n, 返回修改后的string对象引用

    string str = "hello c++! +++";
     // 从位置pos=10处开始删除,直到结尾,即删除 " +++"
        str.erase(10);
        cout << str << endl;   //  “hello c++!”
    // 从位置pos=6处开始,删除4个字符,即删除 "c++!"
        str.erase(6, 4);
        cout << str << endl;   //“hello ”
    

    (2). iterator erase(const_iterator position)
    删除迭代器位置处的单个字符, 并返回下个元素的迭代器

    string str = "hello c++! +++";
      // 删除"+++"前的一个空格
    str.erase(str.begin()+10);
    cout << str << endl;   //"hello c++!+++"
    

    (3). iterator erase(const_iterator first, const_iterator last)
    删除迭代器[first, last)区间的所有字符,返回一个指向被删除的最后一个元素的下一个字符的迭代器

    string str = "hello c++! +++";
     // 删除" +++"
    str.erase(str.begin() + 10, str.end());
    cout << str << endl;   // "hello c++!"
    

    8. string的字符替换

    1. string& replace(size_t pos, size_t n, const char *s);//将当前字符串从pos索引开始的n个字符,替换成字符串s
    2. string& replace(size_t pos, size_t n, size_t n1, char c); //将当前字符串从pos索引开始的n个字符,替换成n1个字符c
    3. string& replace(iterator i1, iterator i2, const char* s);//将当前字符串[i1,i2)区间中的字符串替换为字符串s
    
    string s1("hello,world!");
    cout<<s1.size()<<endl;                     // 结果:12
    s1.replace(s1.size()-1,1,1,'.');           // 结果:hello,world.
    
    // 这里的6表示下标  5表示长度
    s1.replace(6,5,"girl");                    // 结果:hello,girl.
    
    // s1.begin(),s1.begin()+5 是左闭右开区间
    s1.replace(s1.begin(),s1.begin()+5,"boy"); // 结果:boy,girl.
    

    9. string的大小写转换:tolower()和toupper()函数

    10. string的查找:find

    1. size_t find (constchar* s, size_t pos = 0) const;//在当前字符串的pos索引位置开始,查找子串s,返回找到的位置索引,-1表示查找不到子串
    
    2. size_t find (charc, size_t pos = 0) const;//在当前字符串的pos索引位置开始,查找字符c,返回找到的位置索引,-1表示查找不到字符
    
    3. size_t rfind (constchar* s, size_t pos = npos) const;//在当前字符串的pos索引位置开始,反向查找子串s,返回找到的位置索引,-1表示查找不到子串
    
    4. size_t rfind (charc, size_t pos = npos) const;//在当前字符串的pos索引位置开始,反向查找字符c,返回找到的位置索引,-1表示查找不到字符
    
    5. size_tfind_first_of (const char* s, size_t pos = 0) const;//在当前字符串的pos索引位置开始,查找子串s的字符,返回找到的位置索引,-1表示查找不到字符
    
    6. size_tfind_first_not_of (const char* s, size_t pos = 0) const;//在当前字符串的pos索引位置开始,查找第一个不位于子串s的字符,返回找到的位置索引,-1表示查找不到字符
    
    7. size_t find_last_of(const char* s, size_t pos = npos) const;//在当前字符串的pos索引位置开始,查找最后一个位于子串s的字符,返回找到的位置索引,-1表示查找不到字符
    
    8. size_tfind_last_not_of (const char* s, size_t pos = npos) const;//在当前字符串的pos索引位置开始,查找最后一个不位于子串s的字符,返回找到的位置索引,-1表示查找不到子串
    

    11. string的排序:sort(s.begin(),s.end())

    12. string的分割/截取字符串:strtok() & substr()

    //strtok():分割字符串
    char str[] = "I,am,a,student; hello world!";
    const char *split = ",; !";
    char *p2 = strtok(str,split);
      while( p2 != NULL )
        {
            cout<<p2<<endl;
            p2 = strtok(NULL,split);
        }
    

    string s1("0123456789");
    string s2 = s1.substr(2,5);     //参数5表示:截取的字符串的长度
    cout<<s2<<endl;     // 结果为23456
    

     

    参考博客:

    https://blog.csdn.net/qq_37941471/article/details/82107077

    https://blog.csdn.net/u010472607/article/details/80431604

    https://blog.csdn.net/qq_40630246/article/details/103646009

  • 相关阅读:
    甲醛(Formaldehyde)
    Node Embedding
    受限玻尔兹曼机(RBM, Restricted Boltzmann machines)和深度信念网络(DBN, Deep Belief Networks)
    长尾分布,重尾分布(Heavy-tailed Distribution)
    SVD分解与数据压缩
    Batch Normailzation
    Attention Mechanism
    新装的Ubuntu在Nvidia显卡上分辨率不对
    人工神经网络(Artificial Neural Network)
    Xdebug+phpstorm配置
  • 原文地址:https://www.cnblogs.com/USTC-ZCC/p/14661945.html
Copyright © 2011-2022 走看看