• string类型用法大全


    • 使用标准C++中string类,要包含头文件< string >

    string类的构造函数

    //string(const char *s);        用字符串s初始化
    string s1("hello");
    //string(int n,char c);         用n个字符c初始化
    string s2(10, '#');
    //构造空字符串
    string s3;
    //赋值初始化
    string s4="world";
    

    string类的字符操作

    //const char &operator[](int n)const;
    cout<<s1[0];                    //有可能数组越界
    //const char &at(int n)const;
    cout<<s1.at(1);                 //会进行越界检查
    

    string的特性描述

    //int capacity()const;          返回当前容量
    //int max_size()const;          返回string对象中可存放的最大字符串的长度
    //int size()const;              返回当前字符串的大小
    cout<<s1.size();
    //int length()const;            返回当前字符串的长度
    cout<<s1.length();
    //bool empty()const;            当前字符串是否为空
    cout<<s1.empty();
    

    string类的输入输出操作

    //string类重载运算符operator>>用于输入
    string s;
    cin>>s;
    //同样重载运算符operator<<用于输出操作。
    cout<<s;
    //函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'
    '分开。
    getline(cin, s);
    

    string的赋值

    //string &operator=(const string &s);   把字符串s赋给当前字符串
    s1=s2;
    //string &assign(const char *s);        用字符数组类型字符串s赋值
    s1.assign("hello");
    //string &assign(const char *s,int n);  用c字符串s开始的n个字符赋值
    s1.assign("welcom", 3);
    //string &assign(const string &s);      把字符串s赋给当前字符串
    s1.assign(s2);
    //string &assign(int n,char c);         用n个字符c赋值给当前字符串
    s1.assign(10, '#');
    //string &assign(const string &s,int start,int n);把字符串s中从start开始的n个字符赋给当前字符串
    s1.assign(s2, 0, 2);
    

    string的连接 ##

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

    string的比较

    //比较两个字符串是否相等
    bool operator==(const string &s1,const string &s2)const;
    
    运算符">","<",">=","<=","!="均被重载用于字符串的比较;
    int compare(const string &s) const; //比较当前字符串和s的大小
    
    //比较当前字符串从pos开始的n个字符组成的字符串与s的大小
    int compare(int pos, int n,const string &s)const;
    
    //比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
    int compare(int pos, int n,const string &s,int pos2,int n2)const;
    int compare(const char *s) const;
    int compare(int pos, int n,const char *s) const;
    int compare(int pos, int n,const char *s, int pos2) const;
    compare函数在>时返回1,<时返回-1,==时返回0  
    

    string的子串

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

    string的交换

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

    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 char *s, int pos, int n) const;
    //从pos开始查找字符串s中前n个字符在当前串中的位置
    
    int find(const string &s, int pos = 0) const;
    //从pos开始查找字符串s在当前串中的位置
    //查找成功时返回所在位置,失败返回string::npos的值
    
    int rfind(char c, int pos = npos) const;
    //从pos开始从后向前查找字符c在当前串中的位置
    
    int rfind(const char *s, int pos = npos) const;
    int rfind(const char *s, int pos, int n = npos) const;
    
    int rfind(const string &s,int pos = npos) const;
    //从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
    
    int find_first_of(char c, int pos = 0) const;
    //从pos开始查找字符c第一次出现的位置
    
    int find_first_of(const char *s, int pos = 0) const;
    int find_first_of(const char *s, int pos, int n) const;
    
    int find_first_of(const string &s,int pos = 0) const;
    //从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
    
    int find_first_not_of(char c, int pos = 0) const;
    
    int find_first_not_of(const char *s, int pos = 0) const;
    
    int find_first_not_of(const char *s, int pos,int n) const;
    
    int find_first_not_of(const string &s,int pos = 0) const;
    //从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
    
    int find_last_of(char c, int pos = npos) const;
    int find_last_of(const char *s, int pos = npos) const;
    int find_last_of(const char *s, int pos, int n = npos) const;
    int find_last_of(const string &s,int pos = npos) const;
    int find_last_not_of(char c, int pos = npos) const;
    int find_last_not_of(const char *s, int pos = npos) const;
    int find_last_not_of(const char *s, int pos, int n) const;
    
    int find_last_not_of(const string &s,int pos = npos) const;
    //find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
    

    string类的替换函数

    string &replace(int p0, int n0,const char *s);
    //删除从p0开始的n0个字符,然后在p0处插入串s
    
    string &replace(int p0, int n0,const char *s, int n);
    //删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
    
    string &replace(int p0, int n0,const string &s);
    //删除从p0开始的n0个字符,然后在p0处插入串s
    
    string &replace(int p0, int n0,const string &s, int pos, int n);
    //删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
    
    string &replace(int p0, int n0,int n, char c);
    //删除p0开始的n0个字符,然后在p0处插入n个字符c
    

    string类的插入函数

    string &insert(int p0, const char *s);
    string &insert(int p0, const char *s, int n);
    string &insert(int p0,const string &s);
    string &insert(int p0,const string &s, int pos, int n);
    //前4个函数在p0位置插入字符串s中pos开始的前n个字符
    string &insert(int p0, int n, char c);
    //此函数在p0处插入n个字符c
    

    string类的删除函数

    string &erase(int pos = 0, int n = npos);
    //删除pos开始的n个字符,返回修改后的字符串
    

    字符串流处理

    通过定义ostringstream和istringstream对象实现,需要#include <sstream>头文件
    例如:
    
    string input("hello,this is a test");
    istringstream is(input);
    string s1,s2,s3,s4;
    is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
    ostringstream os;
    os<<s1<<s2<<s3<<s4;
    cout<<os.str();
    
  • 相关阅读:
    CSS font 复合属性的顺序
    HTML元素的offsetWidht、clientWidth、scrollWidth属性区别
    javascript document.compatMode属性
    IE下Array.prototype.slice.call(params,0)
    JavaScript中的apply()方法和call()方法使用介绍
    字符串与数组转换
    好的网站链接
    [转]走向视网膜(Retina)的Web时代
    如何对一个公司的供应链管理进行数据分析?
    Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
  • 原文地址:https://www.cnblogs.com/lfyzoi/p/6732045.html
Copyright © 2020-2023  润新知