zoukankan      html  css  js  c++  java
  • string类一些常用函数用法 标签: 函数string 2017-05-15 15:05 30人阅读 评论(0)

    insert
    string的成员函数insert有以下多种重载:

    string &insert(int p0, const char *s);——在p0位置插入字符串s

    string &insert(int p0, const char *s, int n);——在p0位置插入字符串s的前n个字符

    string &insert(int p0,const string &s);——在p0位置插入字符串s

    string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字符串s从pos开始的连续n个字符

    string &insert(int p0, int n, char c);//在p0处插入n个字符c
    iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置

    void insert(iterator it, const_iterator first, const_iteratorlast);//在it处插入从first开始至last-1的所有字符

    void insert(iterator it, int n, char c);//在it处插入n个字符c

    //#include "stdafx.h"//If the vc++6.0, with this line.
    #include <string>
    #include <iostream>
    using namespace std;//
    int main(void){
        string a="1234567890",b="abcdefghijklmn";
        a.insert(3,b,5,4);
        cout << a << endl;
        return 0;
    }

    输出是123fghi4567890.

    erase
    erase函数的原型如下:(1)string& erase ( size_t pos = 0, size_t n = npos );(2)iterator erase ( iterator position );(3)iterator erase ( iterator first, iterator last );也就是说有三种用法:
    (1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
    (2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
    (3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)

    #include <string>
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        string str ("This is an example phrase.");
        string::iterator it;
        //第(1)种方法
        str.erase (10,8);
        cout << str << endl;        // "This is an phrase."
        //第(2)种方法
        it=str.begin()+9;
        str.erase (it);
        cout << str << endl;        // "This is a phrase."
        //第(3)种方法
        str.erase (str.begin()+5, str.end()-7);
        cout << str << endl;        // "This phrase."
        return 0;
    }

    substr
    返回一个从指定位置开始,并具有指定长度的子字符串。
    参数
    start : 必选。所需的子字符串的起始位置。字符串中第一个字符的索引为 0。
    length 可选项。返回的子字符串中包含的字符数。
    备注
    如果 length 为 0 或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到字符串的结尾。

    find
    string中 find()的应用 (rfind() 类似,只是从反向查找)
    原型如下:
    (1)size_t find (const string& str, size_t pos = 0) const; //查找对象–string类对象
    (2)size_t find (const char* s, size_t pos = 0) const; //查找对象–字符串
    (3)size_t find (const char* s, size_t pos, size_t n) const; //查找对象–字符串的前n个字符
    (4)size_t find (char c, size_t pos = 0) const; //查找对象–字符
    结果:找到 – 返回 第一个字符的索引
    没找到–返回 string::npos(-1)
    示例:

    [cpp] view plain copy
    #include <iostream>       // std::cout  
    #include <string>         // std::string  
    int main ()  
    {  
      std::string str ("There are two needles in this haystack with needles.");  
      std::string str2 ("needle");  
    
      // different member versions of find in the same order as above:  
      std::size_t found = str.find(str2);  
      if (found!=std::string::npos)  
        std::cout << "first 'needle' found at: " << found << '
    ';  
    
      found=str.find("needles are small",found+1,6);  
      if (found!=std::string::npos)  
        std::cout << "second 'needle' found at: " << found << '
    ';  
    
      found=str.find("haystack");  
      if (found!=std::string::npos)  
        std::cout << "'haystack' also found at: " << found << '
    ';  
    
      found=str.find('.');  
      if (found!=std::string::npos)  
        std::cout << "Period found at: " << found << '
    ';  
    
      // let's replace the first needle:  
      str.replace(str.find(str2),str2.length(),"preposition");  //replace 用法  
      std::cout << str << '
    ';  
    
      return 0;  
    }  

    结果:
    first ‘needle’ found at: 14
    second ‘needle’ found at: 44
    ‘haystack’ also found at: 30
    Period found at: 51
    There are two prepositions in this haystack with needles

    其他还有 find_first_of(), find_last_of(), find_first_not_of(), find_last_not_of()
    作用是查找 字符串中 任一个字符 满足的查找条件
    string snake1(“cobra”);
    int where = snake1.find_first_of(“hark”);
    返回3 因为 “hark”中 各一个字符 在 snake1–cobra 中第一次出现的是 字符’r’(3为 cobra 中’r’的索引)
    同理:
    int where = snake1.find_last_of(“hark”);
    返回4 因为 “hark”中 各一个字符 在 snake1–cobra 中最后一次出现的是 字符’a’(3为 cobra 中’r’的索引)
    其他同理

    size&&length
    length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符合STL的接口规则,以便用于STL的算法。
    string类的size()/length()方法返回的是字节数,不管是否有汉字。

    reserve
    reverse(s.begin(),s.end()) 颠倒字符串

    strncpy
    strncpy 是 C语言的库函数之一,来自 C语言标准库,定义于 string.h,char *strncpy(char *dest, const char *src, int n),把src所指向的字符串中以src地址开始的前n个字节复制到dest所指的数组中,并返回dest。

    strchr
    char strchr(const char _Str,char _Val)
    char strchr(char _Str,char _Ch)
    查找字符串_Str中首次出现字符_Val的位置
    说明:返回首次出现_Val的位置的指针,返回的地址是被查找字符串指针开始的第一个与Val相同字符的指针,如果Str中不存在Val则返回NULL。
    返回值:成功则返回要查找字符第一次出现的位置,失败返回NULL

  • 相关阅读:
    JS 知识点补充
    JS 数据之间类型的转化
    JS 数据的类型
    数据结构--数组、单链表和双链表介绍 以及 双向链表
    数据结构--队列
    数据结构--栈
    24. 两两交换链表中的节点
    23. 合并K个排序链表
    22. 括号生成
    21. 合并两个有序链表
  • 原文地址:https://www.cnblogs.com/xljxlj/p/7183663.html
Copyright © 2011-2022 走看看