string(1)

为什么学习string类?

C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。

标准库中的string类

auto和范围for

auto关键字

  • 在早期C/C++中auto的含义是:使用auto修饰的变量,是具有自动存储器的局部变量,后来这个不重要了。C++11中,标准委员会变废为宝赋予了auto全新的含义即:auto不再是一个存储类型指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得。
  • 用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&
  • 当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际只对第一个类型进行推导,然后用推导出来的类型定义其他变量。
  • auto不能作为函数的参数,可以做返回值,但是建议谨慎使用
  • auto不能直接用来声明数组
#include<iostream>
using namespace std;

int func1()
{
	return 10;
}

// auto不能做参数
void func2(auto a)
{
}

// auto可以做返回值,但是建议谨慎使用,因为当函数嵌套时,确定函数类型会变得很复杂
auto func3()
{
	return 3;
}

int main()
{
	int a = 10;
	auto b = a;
	auto c = 'a';
	auto d = func1();
	// 编译报错:rror C3531: “e”: 类型包含“auto”的符号必须具有初始值设定项
	auto e;
	cout << typeid(b).name() << endl;
	cout << typeid(c).name() << endl;
	cout << typeid(d).name() << endl;
	int x = 10;
	auto y = &x;
	auto* z = &x;
	auto& m = x;
	cout << typeid(x).name() << endl;
	cout << typeid(y).name() << endl;
	cout << typeid(z).name() << endl;
	auto aa = 1, bb = 2;
	// 编译报错:error C3538: 在声明符列表中,“auto”必须始终推导为同一类型
	auto cc = 3, dd = 4.0;
	// 编译报错:error C3318: “auto []”: 数组不能具有其中包含“auto”的元素类型
	auto array[] = { 4, 5, 6 };
	return 0;
}

在这里插入图片描述
当数据的类型很长时,使用auto就会变得很便利:

#include<iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
	std::map<std::string, std::string> dict = { { "apple", "苹果" },
	{ "orange","橙子" }, {"pear","梨"} };
   //std::map<std::string, std::string>::iterator it = dict.begin();
	auto it = dict.begin();
	while (it != dict.end())
	{
		cout << it->first << ":" << it->second << endl;
		++it;
	}
	return 0;
}

范围for

  • 对于一个有范围的集合而言,由程序员来说明循环的范围是多余的,有时候还会容易犯错误。因此C++11中引入了基于范围的for循环。for循环后的括号由冒号“ :”分为两部分:第一部分是范围内用于迭代的变量,第二部分则表示被迭代的范围,自动迭代,自动取数据,自动判断结束。
  • 范围for可以作用到数组和容器对象上进行遍历
  • 范围for的底层很简单,容器遍历实际就是替换为迭代器,这个从汇编层也可以看到。

以往对一个数组进行打印:

#include<iostream>
using namespace std;

int main()
{
	int arr[] = { 1,3,5,7,9 };
	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		cout << arr[i] <<"  ";
	}
	cout << endl;
	return 0;
}

使用范围for打印:

#include<iostream>
#include<string>
using namespace std;

int main()
{
	int arr[] = { 1,3,5,7,9 };
	for (auto i:arr)
	{
		cout << i << "  ";
	}
	cout << endl;
	return 0;
}

string类的常用接口

string类对象的常见构造

▲表示重要函数

函数功能
string();▲构造空的string类对象,即空字符串
string (const string& str);▲拷贝构造函数
string (const string& str, size_t pos, size_t len = npos);从pos位置拷贝len个长度的str
string (const char* s);▲用C-string来构造string类对象
string (const char* s, size_t n);从s中第n个位置开始拷贝字符串
string (size_t n, char c);string类对象中包含n个字符c
#include<iostream>
#include<string>
using namespace std;

void Teststring()
{
    string s1;// 构造空的string类对象s1
    string s2("hello world");// 用C格式字符串构造string类对象s2
    string s3(s2);// 拷贝构造s3
    string s4(s2, 2, 3);//拷贝s2第二个位置开始的三个字符
    string s5(s2, 6);//从s2的第6个位置开始拷贝
    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
    cout << s5 << endl;
}

int main()
{
    Teststring();
    return 0;
}

在这里插入图片描述

string类对象的容量操作

▲表示重要函数

函数功能
size▲返回字符串有效字符长度
length返回字符串有效字符长度
max_size返回字符串的最大长度
resize▲将有效字符的个数该成n个,多出的空间用字符c填充
capacity▲返回空间总大小
reserve▲为字符串预留空间
clear▲清空有效字符
empty▲检测字符串释放为空串,是返回true,否则返回false
shrink_to_fit缩小字符串长度,使适应字符串有效长度
  • size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
  • clear()只是将string中有效字符清空,不改变底层空间大小。
  • resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize (size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
  • reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。
#include<iostream>
#inlcude<string>
using namespace std;

void Teststring1()
{
	string s("hello world");
	cout << s.size() << endl;
	cout << s.length() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	// 将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小
	s.clear();
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	// 将s中有效字符个数增加到10个,多出位置用'a'进行填充
	// “aaaaaaaaaa”
	s.resize(10, 'a');
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	// 将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行填充
	// "aaaaaaaaaa\0\0\0\0\0"
	// 注意此时s中有效字符个数已经增加到15个
	s.resize(15);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	// 将s中有效字符个数缩小到5个
	s.resize(5);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;
}

int main()
{
	Teststring1();
	return 0;
}

在这里插入图片描述

#include<iostream>
#inlcude<string>
using namespace std;

void Teststring2()
{
	string s;
	// 测试reserve是否会改变string中有效元素个数
	s.reserve(100);
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	// 测试reserve参数小于string的底层空间大小时,是否会将空间缩小
	s.reserve(50);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
}

int main()
{
	Teststring2();
	return 0;
}

在这里插入图片描述
reserve不会改变底层容量大小

  • 查看编译器的扩容情况
#include<iostream>
#inlcude<string>
using namespace std;

void TestPushBack()
{
	string s;
	size_t sz = s.capacity();
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}

int main()
{
	TestPushBack();
	return 0;
}

在这里插入图片描述

可以发现编译器会自动开辟的空间为16,第二次扩容为2倍,之后都是1.5倍,并且每个编译器的开辟空间情况都不同。

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

▲表示重要函数

函数功能
operator[]▲返回pos位置的字符,const string类对象调用
begin+end▲begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
rbegin+rend▲begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
范围for▲C++11支持更简洁的范围for的新遍历方式
#include<iostream>
#inlcude<string>
using namespace std;

void Teststring4()
{
	string s("hello world");
	// 1. for+operator[]
	for (int i = 0; i < s.size(); i++)
	{
		cout << s[i] << " ";
	}
	cout << endl;
	// 2.迭代器
	string::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it <<" ";
		it++;
	}
	cout << endl;

	// string::reverse_iterator rit = s.rbegin();
	// C++11之后,直接使用auto定义迭代器,让编译器推到迭代器的类型
	auto rit = s.begin();
	while (rit != s.end())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
	//倒着遍历
	string::reverse_iterator rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
}


int main()
{
	Teststring4();
	return 0;
}

在这里插入图片描述
在这里插入图片描述
字符串中的第一个唯一字符-力扣
在这里插入图片描述
思路:创建int数组,利用范围for遍历字符数组,并记录字符出现次数

class Solution {
public:
    int firstUniqChar(string s) {
    int arr[26]={0};
    for(auto ch:s)
    {
        arr[ch-'a']++;
    }
    int count=0;
    for(auto ch:s)
    {
        if(arr[ch-'a'] == 1)
            return count;
        count++;
    }
    return -1;
    }
};

在这里插入图片描述
仅仅反转字母-力扣
在这里插入图片描述
思路:
找到字符串的两端,当left<right时进入循环,当左右端都为字母时进行交换,不为字母时左端++,右端–

class Solution {
public:
    bool IsLetter(char str)
    {
        if((str >= 'a' && str <= 'z') || (str >='A' && str <= 'Z'))
            return true;
        return false;
    }

    string reverseOnlyLetters(string s) {
    int left=0;
    int right=s.size()-1;
    while(left < right)
    {
        while(left < right && !(IsLetter(s[left])))
            left++;
         while(left < right && !(IsLetter(s[right])))
            right--;
        swap(s[left++],s[right--]);  
    }
    return s;
    }
};

在这里插入图片描述

评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值