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.判断输入的用户名和密 ...
随机推荐
- ural 1203. Scientific Conference(动态规划)
1203. Scientific Conference Time limit: 1.0 second Memory limit: 64 MB Functioning of a scientific c ...
- jquery的隐藏
HTML 代码:<form> <input type="text" name="email" /> <input type=&qu ...
- Shell脚本常用判断
-e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L fi ...
- 如何使用ajax(jquery)
以下是我第一次工作时写的ajax: $.ajax({ url: "/spinweb/store/storeHome.do", dataType: 'json', data: { & ...
- Astyle编程语言格式化工具的说明
1.工具->扩展和更新,搜astyle插件,下载安装重启,当前是2.0版本. 2.工具->选项->AStyle Formatter->Edit,填入下面的,点击save,确定. ...
- LeetCode OJ 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- Java 环境搭建的一些问题
1.http://www.eclipse.org/webtools/ eclipse 官网,SE.EE方向是两个不同eclipse 2.tomcat 对eclipse来说是一个插件,需要额外下载 T ...
- [实用]DNS解析命令,静静地学会【转载】
[实用]DNS解析命令,静静地学会 2016-08-04 06:50 一.Windows下的nslookup 简单的查某个域名,那就nslookup toutiao.com,上面是dns地址,下面是解 ...
- 详细,Qt Creator快捷键大全,附快捷键配置方法
一.快捷键配置方法: 进入“工具->选项->环境->键盘”即可配置快捷键. 二.常用默认快捷键: 编号 快捷键 功能 1 Esc 切换到代码编辑状态 2 F1 查看帮助(选中某一类或 ...
- english 释词
english 释词 [amount of & number of]the amount of /the number of指……的数量an amount of/a number of 指“大 ...