展开全部

要想使用标准C++中string类,必须要包含

#include // 注意是,不62616964757a686964616fe78988e69d8331333339663434是,带.h的是C语言中的头文件

using std::string;

using std::wstring;

using namespace std;

下面你就可以使用string/wstring了,它们两分别对应着char和wchar_t。

string和wstring的用法是一样的,以下只用string作介绍:

string类的构造函数:

string(const char *s); //用c字符串s初始化

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

此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常 ;

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函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。

const char *data()const;//返回一个非null终止的c字符数组

const char *c_str()const;//返回一个以null终止的c字符串

int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目

string的特性描述:

int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)

int max_size()const; //返回string对象中可存放的最大字符串的长度

int size()const; //返回当前字符串的大小

int length()const; //返回当前字符串的长度

bool empty()const; //当前字符串是否为空

void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分

string类的输入输出操作:

string类重载运算符operator>>用于输入,同样重载运算符operator<

函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开。

string的赋值:

string &operator=(const string &s);//把字符串s赋给当前字符串

string &assign(const char *s);//用c类型字符串s赋值

string &assign(const char *s,int n);//用c字符串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个字符赋给当前字符串

string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串

string的连接:

string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾

string &append(const char *s); //把c类型字符串s连接到当前字符串结尾

string &append(const char *s,int n);//把c类型字符串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

string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾

string的比较:

bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等

运算符">","=","<=","!="均被重载用于字符串的比较;

int compare(const string &s) const;//比较当前字符串和s的大小

int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小

int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中

//pos2开始的n2个字符组成的字符串的大小

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,

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相似,只不过是从后向前查找

本回答被提问者采纳

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

c语言 java append_C++中append函数的用法和函数定义。谢谢!相关推荐

  1. 一个Java程序中是否只能有一个main函数

    否 1.一个Java程序中不一定只有一个main函数,但至少有一个main函数因为Java虚拟机运行时,是以main方法为入口进入的 2.main函数是程序的入口,总之一个Java程序执行时只会执行一 ...

  2. java有没有求组合的函数_如何在Java 8中使用compose和andThen组合函数

    在这篇文章中,课课家将讨论利用java8的Function接口提供的两个组合函数--compose和andThen来实现函数的组合. 什么是函数组合? 首先需要创建一些小的可重用函数,然后将这些小函数 ...

  3. JAVA面向对象中继承的子父类成员函数的内存图解,以及成员函数中的覆盖的应用.

    JAVA中继承子父类成员函数的使用 我把成员函数理解为就是类里面的功能,或者说是方法. 子父类的成员函数调用可以是这样的 例如: class Fu{void show1(){System.out.pr ...

  4. java datediff_JAVA中的时间操作(附DATEDIFF函数用法)

    http://blog.csdn.net/lujiancs/article/details/7043760 //字符串转化成时间类型(字符串可以是任意类型,只要和SimpleDateFormat中的格 ...

  5. java setdate_java中setDate(Date date)方法和String与Date之间的转换

    importjava.sql.Timestamp;importjava.text.DateFormat;importjava.text.ParseException;importjava.text.S ...

  6. java代码中如何嵌入c语言,Android中Java代码与C的互相调用(JNI的简单使用)

    引言 最近在做项目的时候,接触到JNI,想一想自己第一次接触这个东西的时候,还是好久之前,现在既然接触到了,那我就简单的跟大家讲一讲JNI的基本使用方法. JNI(Java Native Interf ...

  7. 有一个包含主函数的java类_重新认识Java中的程序入口即主函数各组成部分

    主函数的定义: public:代表着该函数访问权限是最大的 static:代表主函数随着类的加载就已经存在了 void:主函数没有具体的返回值 main:不是关键字,但是一个特殊的单词,能够被JVM识 ...

  8. java 转码%2f%_JS和JAVA中常用的编码转码函数

    js中escape,encodeURI,encodeURIComponent函数和unescape,decodeURI和decodeURIComponent函数的功能 1.escape方法对Strin ...

  9. C语言中的指针应用,函数指针,指针函数,结构体中定义函数指针。

    指针函数 指针函数就是一个返回指针的函数,其本质是一个函数,该函数的返回值是一个指针. 声明一个指针函数 int* fun(int x,int y); 例子 typedef struct _Data{ ...

最新文章

  1. umi搭建react+antd项目(四)axios请求数据
  2. 离散数学范式c语言实验报告,离散数学实验报告-利用真值表法求主析取范式及主合取范式的实现...
  3. 小程序 富文本自适应屏幕_自适应文本:跨屏幕尺寸构建可读文本
  4. 【渝粤题库】广东开放大学 电子商务安全与支付 形成性考核
  5. 基于 MySQL + Tablestore 分层存储架构的大规模订单系统实践-架构篇
  6. axure 画小程序效果图_APP详情页如何用Axure画出来
  7. linux在当前目录下创建pic目录,在Linux上创建.SO文件而不使用PIC(与位置无关的代码)(x86 32位)...
  8. HDU 2340 - Obfuscation(dp)
  9. 刷路由器文件服务器打开失败,路由器升级错误引发网页打开失败故障
  10. 一篇文章助你深入理解zookeeper
  11. 七张王牌助你做人做事顺遂成功
  12. android个人日记本论文,手机app移动开发论文-个人心情日记本的设计实现 毕设论文.doc...
  13. 在Firemonkey中实现ListView
  14. ARKit之路-射线检测
  15. 计算机技能大赛简讯内,【报道】2010学西城区职业高中计算机排版技能竞赛简讯...
  16. 单机魔域显示服务器未启动,魔域单机版_开始游戏
  17. 正则表达式 压缩 HTML 字符串
  18. 支付宝退款流程 php,支付宝退款接口对接流程PHP语言
  19. unity3d和建模关系
  20. crc32校验的c语言实现

热门文章

  1. 最好理解的: spring ioc原理讲解,强烈推荐!
  2. 大佬(概率期望DP)
  3. Django Rest Framework -解析器
  4. Oracle序列更新为主键最大值
  5. 在操作系统重启后恢复应用程序的工作状态
  6. Python冒泡排序(4)
  7. php常见的面试题目
  8. 12.13记录//QQDemo示例程序源代码
  9. POJ 1088-滑雪
  10. Bash : 索引数组