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.判断输入的用户名和密 ...
随机推荐
- PHP 流程
登录页面 <body> <form action="loginchuli.php" method="post"> <div> ...
- 3. JavaScript 数据类型
1. 主要类型 字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空(Null).未定义(Undefined). JavaScript 拥 ...
- oracle行转列函数
- VirtualBox 不能为虚拟电脑打开一个新的任务 可能的解决方案
1. 在虚拟机上右键,清除保存状态 2.Cannot load R0 module C:\Program Files\Oracle\VirtualBox/VBoxDD2R0.r0: SUPR3Load ...
- Network view
network view 组件用来在局域网之内去同步一个游戏物体的组件属性,只响应创建它的那个客户端事件
- sql 在将 nvarchar 值 转换成数据类型 int 时失败。
假设有存储过程:proc_test2 create proc proc_test2 @Id int begin as declare @sql varchar(max) @sql = 'select ...
- JavaScript高级程序设计:第六章
第六章 面向对象的程序设计 一.理解对象 1.属性类型: ECMAScript中有两种属性:数据属性和访问器属性. (1)数据属性: 数据属性包含一个数据值的位置.在这个位置可以读取和写入值.数据属性 ...
- thinkphp 实现微信公众号开发(二)--实现自定义菜单
IndexController.class.php <?php namespace Home\Controller; use Think\Controller; class IndexContr ...
- 使用CodeFirst实现动态建库
一.业务分析 以我们平时注册今目标为例,我们在注册今目标的过程中,具体步骤是这样的: 图1 今目标登陆流程 详细解释一下: 第一步:注册界面.输入手机号或者邮箱,点击确定进入基本信息界面. 第二步:基 ...
- MFC下对串口的操作以及定时器的调用
最近研究了一下MFC下对串口的操作,测试了一下对设备的读写. 1.打开串口 GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(FALSE); m_hComm = ...