以下是阿鲤对c++中string类学习的总结,希望对大家有所帮助;若有误请慷慨指出。
一:string类的介绍
二:string类的常用接口说明
一:string类的介绍
1.1 首先请大家看一下string类在c++官网中的介绍。c++官网对string类介绍入口
Strings are objects that represent sequences of characters.
The standard string class provides support for such objects with an interface similar to that of a standard container of bytes, but adding features specifically designed to operate with strings of single-byte characters.
The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type, with its default char_traits and allocator types (see basic_string for more info on the template).
Note that this class handles bytes independently of the encoding used: If used to handle sequences of multi-byte or variable-length characters (such as UTF-8), all members of this class (such as length or size), as well as its iterators, will still operate in terms of bytes (not actual encoded characters).
1.2 翻译:
1. 字符串是表示字符序列的类
2. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作单字节字符字符串的设计特性。
3. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits和allocator作为basic_string的默认参数(根于更多的模板信息请参考basic_string)。
4. 注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。
1.2 总结:
1. string是表示字符串的字符串类
2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
3. string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator> string;
4. 不能操作多字节或者变长字符的序列。
注意:
在使用string类时,必须包含头文件以及using namespace std;
二:string类的常用接口说明
2.1:string类的常见构造
函数名称 | 功能说明 |
string | 构造sting类对象,及空字符串 |
string(const char*s) | 用C-string来构造string类对象 |
string(size_t n, char c) | string类对象中包含n个字符c |
string(const string &s) | 拷贝构造 |
string(const string &s, size_t n) | 去掉s中前n个字符来构造写的string类对象 |
eg:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
string s2("zhaoyun");
string s3(5, 'n');
string s4(s2);
string s5(s2,3);
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << s5 << endl;
system("pause");
return 0;
}
输出:
2.2:string类对象的容量的操作
函数名称 | 功能说明 |
size_t size() const | 返回字符串有效字符长度 |
size_t length() const | 返回字符串有效字符长度 |
size_t capacity() const | 返回空间总大小 |
bool empty() const | 检测字符串释放为空串,是返回true,否则返回false |
void clear() | 清空有效字符 |
void resize(size_t n, char c) | 将有效字符的个数该成n个,多出的空间用字符c填充 |
void resize(size_t n) | 将有效字符的个数该成n个,多出的空间用字符0填充 |
void reserve(size_t res_arg = 0) | 为字符串预留 |
eg:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s2("zhaoyun");
cout << s2 << endl;//打印s2
cout << s2.size() << endl;//计算有效字符串长度
cout << s2.length() << endl;//计算有效字符串长度
cout << s2.capacity() << endl;//查看容量
cout << s2.empty() << endl;//判空
s2.clear();//清空
cout << s2 << endl;//打印s2
cout << s2.size() << endl;//计算有效字符串长度
s2.resize(6, 'n');//将有效字符的个数该成n个,多出的空间用字符n填充
cout << s2 << endl;//打印s2
cout << s2.size() << endl;//计算有效字符串长度
s2.resize(10);//将有效字符的个数该成n个,多出的空间用字符0填充
cout << s2 << endl;//打印s2
cout << s2.size() << endl;//计算有效字符串长度
s2.reserve(100);//扩容
cout << s2.capacity() << endl;//查看容量
system("pause");
return 0;
}
输出:
解释+注意
1:size 和 strlen的作用一模一样;
2:清空不会减少容量;
3:string的扩容是以16字节进行扩容的,但是第最小容量是15;所以其容量就是扩容的次数乘16-1;
4:resize(10);后面是有0的只不过是没打印出来
2.3: string类对象的访问操作
函数名称 | 功能说明 |
char& operator[] ( size_t pos ) | 返回pos位置的字符,const string类对象调用 |
const char& operator[] ( size_t pos ) const | 返回pos位置的字符,非const string类对象调用 |
eg:
#include<iostream>
#include<string>
using namespace std;
int main()
{
const char *s2 = "zhaoyun";
char s1[] = { "zhaoyun" };
cout << s1[4] << endl;
cout << s2[4] << endl;
system("pause");
return 0;
}
输出:
2.4:string类对象的修改操作
函数名称 | 功能说明 |
void push_back(char c) | 在字符串后尾插字符c |
string& append(const char* s) | 在字符串后追加一个字符串 |
string& operator+=(const string& str) | 在字符串后再追加str |
string& operator+=(const char*s) | 在字符串后追加C个数字符串 |
string& operator+=(char c) | 在字符串后追加字符c |
const char* c_str()const | 返回C格式字符串 |
size_t find (char c, size_t pos = 0)const | 从字符串pos位置开始往后找字符c,找到返回该字符的位置 |
size_t rfind(char c, size_t pos = npos) | 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置 |
string substr(size_t pos = 0, size_t n = npos)const | 在str中从pos位置开始,截取n个字符,然后将其返回 |
eg:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("zhao yun");
string s2(" man");
cout << s1 << endl;
s1.append(" is toug");
cout << s1 << endl;
s1.push_back('h');
cout << s1 << endl;
s1 += s2;
cout << s1 << endl;
s1 += " ha ha ha";
cout << s1 << endl;
cout << s1.c_str() << endl;
cout << "The position of g is " << s1.find('g') << endl;
cout << "The position of g is " << s1.find('o',5) << endl;
cout << s1.substr(5, 12) << endl;
system("pause");
return 0;
}
输出:
注意:
1. 在string尾部追加字符时,s.push_back(c) / s.append(1, c) / s += 'c'三种的实现方式差不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。
2. 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好。
2.5:string类非成员函数
函数名称 | 功能说明 |
operator+ | 尽量少用,效率低 |
operator>> | 输入运算符重载 |
operator<< | 输出运算符重载 |
getline | 解决不能空格的输出 |
relational operators | 大小比较 |
eg:(在此只对getline做介绍)
1:getline
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
string s2;
getline(cin, s1);
cin >> s2;
cout << s1 << endl;
cout << s2 << endl;
system("pause");
return 0;
}
输入输出结果:
string类的简单实现
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
class MyString
{
char *m_data;
public:
MyString():
m_data(new char[1])
{
m_data = '\0';
}
MyString(const char *str)
{
if (nullptr == str)
{
str = new char[1];
str = '\0';
}
m_data = new char[strlen(str) + 1];
strcpy(m_data, str);
}
~MyString()
{
delete m_data;
m_data = nullptr;
}
MyString(const MyString &string)://使用引用是为了无穷拷贝
m_data(new char[strlen(string.m_data) + 1])
{
strcpy(m_data, string.m_data);
}
MyString& operator=(const MyString &string)//返回引用是为了支持连等,和减少拷贝和析构次数
//若不返回引用,则会再赋值的时候创建一个临时对象并西贡。
{
if (this == &string)
{
return *this;
}
delete[]m_data;//~MyString();
m_data = nullptr;
m_data = new char[strlen(string.m_data) + 1];
strcpy(m_data, string.m_data);
return *this;
}
/*
MyString& operator=(const MyString &string)
{
//strtmp是一个局部变量,出了if语句就会将他释放掉,所以我们交换strtmp和m_data,这样就会使得自动释放,还不会因为内存
//申请失败而爆发异常,还改了原来的数据。
if (this != &string)
{
MyString strTmp(string);
char *pTmp = strTmp.m_data;
strTmp.m_data = m_data;
m_data = pTmp;
}
return *this;
}
*/
const char *c_str()const
{
return m_data;
}
};
测试代码:
#include"MyString.h"
int main()
{
const char *s = "wang rui";
MyString str1(s);
MyString str2(str1);
MyString str3;
str3 = str1 = str2;
}
好啦,以上就是阿鲤对string中的一些学习总结啦