C++:string类的使用
类
<string>
std::string
String类的定义 , 其也是个模板类
typedef basic_string<char> string;
String class
Strings是代表一串字符的对象
标准string类提供了类似于标准容器的接口,但是增加处理字符串所需的特殊的特征
string是basic_string 类模板使用char类型作为字符类型实现的类。使用默认的字符特征和分配器类型。
成员类型
|
成员类型 |
定义 |
|
value_type |
char |
|
traits_type |
char_traits<char> |
|
allocator_type |
allocator<char> |
|
reference |
char& |
|
const_reference |
const char& |
|
pointer |
char* |
|
const_pointer |
const char* |
|
iterator |
一种可以随机访问的遍历器 (可转换为const_iterator) |
|
const_iterator |
随机访问 const char类型的遍历器 |
|
reverse_iterator |
倒序访问遍历器 |
|
const_reverse_iterator |
倒序访问const 遍历器 |
|
difference_type |
ptrdiff_t 保存指针相减后的值得类型 与机器相关 通常定义为 long int |
|
size_type |
Member functions
(constructor)构造函数
|
default (1) |
string();构造一个空串 |
|
copy (2) |
string (const string& str);拷贝构造 |
|
substring (3) |
string (const string& str, size_t pos, size_t len = npos); //使用 string 的 从 pos 位置开始的 len长的字符串构造,默认到串尾 |
|
from c-string (4) |
string (const char* s);//使用 c风格字符串构造 |
|
from buffer (5) |
string (const char* s, size_t n);使用 C 风格字符串构造 |
|
fill (6) |
string (size_t n, char c); //使用字符 c 构造一个长 n 的串 |
|
range (7) |
template <class InputIterator> string (InputIterator first, InputIterator last); 使用遍历器构造 |
|
Initializer list (8) |
string (initializer_list<char> il); |
|
move (9) |
string (string&& str) noexcept; |
Construct string object (public member function )
String destructor (public member function )
重载 = 操作符
String assignment (public member function )
Iterators: 遍历器
begin 返回指向串首的遍历器
Return iterator to beginning (public member function )
end 返回指向串尾的遍历器(指向最后一个字符的下一个位置)
Return iterator to end (public member function )
rbegin 倒序遍历 指向串最后一个字符 ,向前遍历
Return reverse iterator to reverse beginning (public member function )
rend 倒序遍历 指向第一个字符的下一个位置
Return reverse iterator to reverse end (public member function )
cbegin常遍历器 指向串首
Return const_iterator to beginning (public member function )
cend 常遍历器 指向串尾的下一个字符
Return const_iterator to end (public member function )
crbegin常倒序遍历 指向串首
Return const_reverse_iterator to reverse beginning (public member function )
crend 长倒序遍历 指向串尾
Return const_reverse_iterator to reverse end (public member function )
Capacity: 容量
size 返回字节数的字节数
Return length of string (public member function )
length 返字符数
Return length of string (public member function )
max_size 返回最大能
Return maximum size of string (public member function )
resize 调整string 大小
Resize string (public member function )
capacity 实际容量
Return size of allocated storage (public member function )
reserve 字符串倒序
Request a change in capacity (public member function )
clear 字符串清空
Clear string (public member function )
empty 测试字符串是否为空
Test if string is empty (public member function )
shrink_to_fit 收缩string当前容量
Shrink to fit (public member function )
Element access:
operator[] 通过索引访问string中的字符
Get character of string (public member function )
at 获取 string 中的字符
Get character in string (public member function )
back 获取string 总的最后一个字符
Access last character (public member function )
front 访问string 的第一个字符
Access first character (public member function )
Modifiers:
operator+= 在string 末尾连接一个字符串
Append to string (public member function )
append 在string 末尾连接一个字符串
Append to string (public member function )
push_back 在string 末尾连接一个字符
Append character to string (public member function )
assign 为string 分配容量
Assign content to string (public member function )
insert 在string 中插入 一个 字符
Insert into string (public member function )
erase 在string 中删除一个字符
Erase characters from string (public member function )
replace 替换某位置的的字符
Replace portion of string (public member function )
swap 交换俩string 的数据
Swap string values (public member function )
pop_back从string 末尾删除一个字符
Delete last character (public member function )
String operations: string 操作
c_str 返回与 string 中存放的字符串等价的 c string
Get C string equivalent (public member function )
data 获取 string 中存放的data
Get string data (public member function )
get_allocator 获取分配器
Get allocator (public member function )
copy 从string 中拷贝字符串
Copy sequence of characters from string (public member function )
find 查找
Find content in string (public member function )
rfind 逆序查找
Find last occurrence of content in string (public member function )
Find character in string (public member function )
Find character in string from the end (public member function )
Find absence of character in string (public member function )
Find non-matching character in string from the end (public member function )
substr 生成子串
Generate substring (public member function )
compare 比较字符串
Compare strings (public member function )
Member constants
Maximum value for size_t (public static member constant )
Non-member function overloads
Concatenate strings (function )
Relational operators for string (function )
Exchanges the values of two strings (function )
Extract string from stream (function )
Insert string into stream (function )
Get line from stream into string (function )
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "构造函数的测试:" <<endl;
string sa = "hello world";
string sb(sa);
string sc("hello world");
string sd(,'@');
string se(sa.begin(),sa.end()); cout << sa <<endl;
cout << sb <<endl;
cout << sc <<endl;
cout << sd <<endl;
cout << se <<endl; //只有 C++11 标准才支持 cbegin() cend() crbegin() 和 crend() 函数
cout << "遍历器的使用:" << endl;
/*正序遍历*/
for( string::iterator p = sa.begin() ; p!= sa.end() ; p++ )
cout << *p ;
cout << endl;
//常量正序遍历
for( string::const_iterator p = sa.begin() ; p!= sa.end() ; p++ )
cout << *p ;
cout << endl;
//倒序遍历
for( string::reverse_iterator p = sa.rbegin() ; p!= sa.rend() ; p++ )
cout << *p ;
cout << endl;
//倒序常量遍历
for( string::const_reverse_iterator p = sa.rbegin() ; p!=sa.rend() ; p++ )
cout << *p;
cout << endl; cout << "与容量相关的函数的使用:" << endl;
cout << "size " << sa.size() << endl;
cout << "len " << sa.length() << endl;
cout << "capacity " << sa.capacity() << endl;
cout << "max_size " << sa.max_size() << endl; cout << "元素的访问" << endl;
for(string::size_type i = ; i < sa.length() ; i++ )
cout << sa.at(i) << " " ;
cout << endl; cout << sa.c_str() <<endl;
cout <<sa.data() << endl; return ;
}
C++:string类的使用的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- java基础复习:final,static,以及String类
2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...
- String类和StringBuffer类的区别
首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...
- 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明
Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
随机推荐
- CentOS 6下的VPN搭建
PPTP 全称为 Point to Point Tunneling Protocol — 点到点隧道协议,是VPN协议中的一种.虚拟专用网(VPN)被定义为通过一个公用网络(通常是因特网)建立一个临时 ...
- FreeMarker 实例
1.jar包:freemarker-2.3.19.jar,将jar拷贝到lib目录下: 2.新建Web项目:TestFreeMarker 在web目录下新建ftl文件夹: 在ftl下新建模版文件ftl ...
- 【Python之路】第一篇--Linux基础命令
pwd 命令 查看”当前工作目录“的完整路径 pwd -P # 显示出实际路径,而非使用连接(link)路径:pwd显示的是连接路径 . 表示当前目录 .. 表示上级目录 / 表示根目录 ls ...
- IIS启用GZip压缩
IIS启用GZip压缩,是提高网站速度和减轻服务器负载的一个优化手段和方法,经测试,网站启用GZip压缩后,速度快了3倍!而配置起来也相当的简单,因此被广大网站管理员使用.本文将详细介绍如何在IIS启 ...
- python初识1
作者:武沛齐 出处:http://www.cnblogs.com/wupeiqi/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接. 安装Pyt ...
- Jekyll: .md to .html with self defined themes..
theme is from here $ gem install jekyll bundler ~ $ jekyll new my-awesome-site ~ $ cd my-awesome-sit ...
- ssl证书验证
当我们在访问https网站时,浏览器就会自动下载该网站的SSL证书,并对证书的安全性进行检查. 其他概念不说了,有效期之类的验证也不说了.只说数字证书的真实性和可信性验证. 1.CA下发给网站的证书是 ...
- 《JavaScript高级程序设计》读书笔记 ---函数
函数函数对任何语言来说都是一个核心的概念.通过函数可以封装任意多条语句,而且可以在任何地方.任何时候调用执行.ECMAScript 中的函数使用function 关键字来声明,后跟一组参数以及函数体. ...
- SB淘宝api的奇葩问题! 一则服务器无法访问淘宝api
<?xml version="1.0" encoding="utf-8" ?><error_response><code>3 ...
- MySQL慢日志查询全解析:从参数、配置到分析工具【转】
转自: MySQL慢日志查询全解析:从参数.配置到分析工具 - MySQL - DBAplus社群——围绕数据库.大数据.PaaS云,运维圈最专注围绕“数据”的学习交流和专业社群http://dbap ...