C++ std::string 不可初始化为NULL及基本用法
来源:互联网 发布:中国四大财阀 知乎 编辑:程序博客网 时间:2024/06/15 16:34
偶然看到一个问题,顺便总结一下std::string
C++ basic_string::_S_construct null not valid
stackoverflow例子
std::string 字符串不可以初始化为NULL,虽然能通过编译,但是会出现运行错误。
std::string 使用以下两种初始化为空的方式:
std::string s;//执行默认构造函数std::string s = "";//执行拷贝构造函数
清空字符串,参考cplusplus.com帮助文档
std::string s = "123";s.clear();
Clear string
Erases the contents of the string, which becomes an empty string (with a length of 0 characters).
另:std::string 不可以与null相比较,可以与”“比较,使用empty()函数,length() == 0
总结一下string
标准C++库字符串类std::string的用法
/#include
std::string s1;
std::string s3(s2);
std::string s2(“this is a string”);
begin 得到指向字符串开头的Iterator
end 得到指向字符串结尾的Iterator
rbegin 得到指向反向字符串开头的Iterator
rend 得到指向反向字符串结尾的Iterator
size 得到字符串的大小
length() 和size函数功能相同
max_size 字符串可能的最大大小
capacity 在不重新分配内存的情况下,字符串可能的大小
empty 判断是否为空
operator[] 取第几个元素,相当于数组
c_str 取得C风格的const char* 字符串
data 取得字符串内容地址
operator= 赋值操作符
reserve 预留空间
swap 交换函数
insert 插入字符
append 追加字符
push_back 追加字符
erase 删除字符串
clear 清空字符容器中所有内容
resize 重新分配空间
assign 和赋值操作符一样
replace 替代
copy 字符串到空间
find 查找,返回基于0的索引号
rfind 反向查找
find_first_of 查找包含子串中的任何字符,返回第一个位置
find_first_not_of 查找不包含子串中的任何字符,返回第一个位置
find_last_of 查找包含子串中的任何字符,返回最后一个位置
find_last_not_of 查找不包含子串中的任何字符,返回最后一个位置
substr(n1,len) 得到字符串从n1开始的长度为len的子串
比较字符串(支持所有的关系运算符)
compare 比较字符串
operator+ 字符串链接
operator+= += 操作符
operator== 判断是否相等
operator!= 判断是否不等于
operator< 判断是否小于
operator>> 从输入流中读入字符串
operator<< 字符串写入输出流
getline 从输入流中读入一
- C++ std::string 不可初始化为NULL及基本用法
- 日期初始化为null
- 【ThinkingInJava】8、对象的初始化为null,基本类型默认初始化为0
- 为何指针初始化为NULL
- std::string的基本用法
- 指针初始化为NULL的作用
- 对象初始化为null的好处
- 指针初始化为NULL的作用
- 不能将类型为‘std::string&’的非 const 引用初始化为类型为‘const char*’的临时变量
- C++/STL std::string 的用法
- C字符串数组初始化为空串
- C字符串数组初始化为空串
- System.DateTime初始化为null的思维痕迹
- System.DateTime初始化为null的思维痕迹
- System.DateTime初始化为null的思维痕迹 .
- android std::string(NULL) crash
- std::string用法详解
- std::string的用法
- 【Java】Java的继承和多态详解
- web.xml加载顺序及Spring包扫描注意
- 基于 Node.js 搭建 HTTP/2服务
- linux下的多进程通信(IPC)原理及实现方案(管道、队列、信号量、共享内存)
- 沟通能力之PPT综合能力方法
- C++ std::string 不可初始化为NULL及基本用法
- [在线Demo]使用Hibernate多租户实现SaaS服务
- 举例说明JMETER的用法-某网站压力测试
- 移动端适配方案
- 安装spark-1.5.0-cdh5.5.2所踩过的坑
- ftp自动上传
- RCNN学习路线推荐(4)---深度学习
- 严重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
- Maven Dependencies 的.jar路径错误