string(上)

本文详细介绍了C++中的string类,包括各种构造方法、容量操作(如size(),capacity(),empty(),clear(),reserve(),resize()等)、访问和遍历(如operator[],begin(),end(),rangefor等),以及修改操作(如append(),rfind(),substr())。string类实际上是basic_string模板类的别名,主要用于处理字符串数据。
摘要由CSDN通过智能技术生成

目录

string类简介:

string常见接口:

string类对象的常见构造:

string():

string(const char *s):

string(size_t n,char c):

string(const string&s):

string类对象的容量操作:

size:

capacity:

empty:

clear:

reserve:

resize:

string类对象的访问及遍历操作:

operator[]:

begin+end:

范围for:

string类对象的修改操作:

append:

rfind:

substr:


string类简介:

cpp中string类文档:https://cplusplus.com/reference/string/string/?kw=stringicon-default.png?t=N7T8https://cplusplus.com/reference/string/string/?kw=string

  • string是表示字符串的字符串类
  • 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作
  • string在底层实际是:basic_string模板类的别名,typedef basic_string<char,char_traits, allocator> string;
  • 不能操作多字节或者变长字符的序列

string常见接口:

string类对象的常见构造:

default (1)string();
from c-string (2)string (const char* s);
fill (3)string (size_t n, char c);
copy (4)string (const string& str);

string():

构造空的string类对象,即空字符:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1;
	cout << s1 << endl;
	return 0;
}

string(const char *s):

用C-string来构造string类对象:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1("hello world");
	cout << s1 << endl;
	return 0;
}

string(size_t n,char c):

string类对象中包含n个字符c:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1(10,'x');
	cout << s1 << endl;
	return 0;
}

string(const string&s):

拷贝构造函数:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1("hello world");
    string s2(s1);
	cout << s1 << endl;
	return 0;
}

string类对象的容量操作:

size:

返回字符串的长度,以字节为单位

void Test()
{
	string str("hello world");
	cout << str.size() << endl;
}

capacity:

返回当前为字符串分配的存储空间的大小,以字节表示

这个容量不一定等于字符串长度。它可以等于或大于,额外的空间允许对象在向字符串中添加新字符时优化其操作

void Test()
{

	string str("hello world");
	cout << str.capacity() << endl;
}

empty:

返回字符串是否为空(即其长度是否为0)

void Test()
{
	string str("hello world");
	bool x = str.empty();
	cout << x << endl;
}

clear:

擦除字符串的内容,使其成为空字符串(长度为0个字符)

void Test()
{
	string str("hello world");
	str.clear();
	cout << str << endl;
}

reserve:

请求字符串容量适应最大长度为n个字符的计划大小更改
如果n大于当前字符串容量,则该函数使容器将其容量增加到n个字符(或更大)
在所有其他情况下,收缩字符串容量被视为非绑定请求:容器实现可以自由地进行优化,并使字符串的容量大于n

//仅仅开辟空间
int main()
{
	string s;
	s.reserve(100);
	size_t sz1 = s.capacity();
	size_t sz2 = s.size();
	cout << sz1 << endl;
	cout << sz2 << endl;

	return 0;
}

resize:

将字符串大小调整为n个字符的长度
如果n小于当前字符串长度,则将当前值缩短到前n个字符,删除第n个字符以外的字符
如果n大于当前字符串长度,则通过在末尾插入尽可能多的字符来扩展当前内容,以达到n的大小。如果指定了c,则新元素被初始化为c的副本,否则,它们是值初始化的字符(空字符)

int main()
{
	string s1;
	s1.resize(10);
	s1[0] = 0;
	s1[1] = 1;
    s1[2] = 2;
	s1[3] = 3;
	s1[4] = 4;

	string s2("hello world");
	s2.resize(20, 'x');
	s2.resize(5);
	cout << s2 << endl;

	return 0;
}

string类对象的访问及遍历操作:

operator[]:

返回pos位置的字符,const string类对象调用

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1("hello world");
	size_t i = 0;
	for (i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;
	return 0;
}

begin+end:

begin获取一个字符的迭代器+end获取最后一个字符下一个位置的迭代器

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	string s1("hello world");
	string::iterator it1 = s1.begin();
	while (it1 != s1.end())
	{
		cout << *it1 <<" ";
		++it1;
	}
	cout << endl;
	return 0;
}

范围for:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    string s1("hello world");
    for (auto e : s1)
	{
		cout << e << " ";
	}
	cout << endl;
    return 0;
}

string类对象的修改操作:

append:

通过在当前值的末尾附加附加字符来扩展字符串

void Test()
{
	string str1("hello world");
	string str2("hello world");
	cout << str1 << endl;

	str1.append(str2);
	cout << str1 << endl;

	str1.append("xxxxxx");
	cout << str1 << endl;

	str1.append(10, 'x');
	cout << str1 << endl;

	str1 += 'x';
	cout << str1 << endl;
}

rfind:

在字符串中搜索由其参数指定的序列的最后一次出现。
当指定pos时,搜索只包括从位置pos或之前开始的字符序列,忽略从位置pos之后开始的任何可能的匹配

void Test()
{    
    string str("hello world");
    size_t pos = str.rfind('w');
    cout << pos << endl;
}

substr:

返回一个新构造的字符串对象,其值初始化为此对象的子字符串的副本。
子字符串是对象的一部分,从字符位置pos开始,跨越len个字符(或直到字符串结束,以先到者为准)

void Test()
{
	string str("hello world");
	size_t pos = str.rfind('w');
	string suffix = str.substr(pos);
	cout << suffix << endl;
}

  • 27
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值