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.判断输入的用户名和密 ...
随机推荐
- Python 学习笔记9
梦想和我只有一层纸的距离. 接着看Python类:http://www.pythondoc.com/pythontutorial3/classes.html
- Ext 下拉框联动第一次显示不正常的问题
做下拉框联动,异步加载数据,第一次显示时数据不准确,不要在combo_2的下拉框直接绑定store,在combo_1的改变事件里调用下面的方法 function GetAllCustomerBrand ...
- android之服务service
service的概念: 官方定义:在后台长时间的操作,没有用户界面,不与用户进行交互,在默认的情况下,service运行在应用程序进程的主线程中,如果需要在Service中处理一些网络连接等耗时操作, ...
- 笔记整理--Linux守护进程
Linux多进程开发(三)进程创建之守护进程的学习 - _Liang_Happy_Life__Dream - 51CTO技术博客 - Google Chrome (2013/10/11 16:48:2 ...
- 编写一个闹钟和定时关机工具(MFC VS2010)
这个小工具在自己生活当中能用到,运行软件以后,会显示当前的系统时间,然后你可以设定时间,再选择是定时响铃还是关机.截图如下: 前言:本程序采用visual studio 2010 ,对话框类型的应用程 ...
- JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式)
什么是序列化 java中的序列化(serialization)机制能够将一个实例对象的状态信息写入到一个字节流中,使其可以通过socket进行传输.或者持久化存储到数据库或文件系统中:然后在需要的时候 ...
- STM32F207 两路ADC连续转换及GPIO模拟I2C给MT9V024初始化参数
1.为了更好的方便调试,串口必须要有的,主要打印一些信息,当前时钟.转换后的电压值和I2C读出的数据. 2.通过GPIO 模拟I2C对镁光的MT9V024进行参数初始化.之前用我以前公司SP0A19芯 ...
- http header cache-control (request和response区别)
摘要:(1)网络服务会根据 request的header中的 cache-control策略设置response的cache-control策略 1 response cache-control 和 ...
- 开机启动 sh
我编辑 /etc/init.d/vsftpd 将 nohup java -jar fff.jar & 放在前面 .就执行了
- 在JavaScript中创建命名空间的几种写法
在JavaScript中全局变量经常会引起命名冲突,甚至有时侯重写变量也不是按照你想像中的顺序来的,可以看看下面的例子: var sayHello = function() { return 'Hel ...