C++(11)——string

      前面通过前面11篇文章介绍了C++中的各项基本知识。从本篇文章开始,将对C++中的string中的各项内容进行介绍:

目录

1.string类对象的常见构造:

2. string类对象的赋值操作:

3. string类对象的访问与遍历:

3.1 string类对象的访问:

3.2 string类对象的遍历:

4. 迭代器:

5.string类对象的容量操作:

5.1 size和capacity:

5.2 reserve和resize:

6. 对于对象中内容的修改:

6.1 push_back:

6.2 append:

6.3 operator +=:


1.string类对象的常见构造:

首先,对于string的使用,需要引入头文件:

#include<string>

        对于string类中的常见构造,可以通过string::string - C++ Reference (cplusplus.com)进行查询。具体如下:

       文章将对上述图片中某几个常用的函数进行介绍,其他函数的使用方法可以通过上方网址进行查阅。对于上述函数的作用,主要是用于创建一个string类对象。例如,创建一个空的string类对象的方法为:

int main()
{
	string s1;  //创建一个空的string类对象
	return 0;
}

    如果想创建一个包含内容的string类对象,其方法为:

string s2("hello world");

上述方法是使用了一个常量字符串来初始化这个string类对象。对应了上图中给出的:

string(const char* str)

在上图中,有一个函数中的参数包含了引用,即:

string( const char& str)

此函数的功能可以理解为一个拷贝构造函数,使用方法有如下两种:

string s3 = s2;
string s4(const char& s2);

对于string类对象的打印,直接使用cout即可,例如:

int main()
{
	string s1;  //创建一个空的string类对象
	string s2("hello world");
	string s3 = s2;

	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	return 0;
}

2. string类对象的赋值操作:

 对于string类对象的赋值操作,可以分为三类:

string& operator= (const string& str);
	
string& operator= (const char* s);
	
string& operator= (char c);

对于上面三类赋值操作的使用,如下所示:

s1 = s2;
cout << s1 << endl;

s1 = 'x';
cout << s1 << endl;

s1 = "hello worle";
cout << s1 << endl;

运行结果如下:

3. string类对象的访问与遍历:

3.1 string类对象的访问:

对于string类对象的访问,通过operator[]来实现。对于此函数,主要有两种形式:

char& operator[] (size_t pos);

const char& operator[] (size_t pos) const;

对于二者的不同点,可以认为第一种可读可写,但是第二种只能读。例如,访问上述代码中,s2中下标为7的字符,即:

cout << s2[7] << endl;

运行结果如下:

上面说到,对于第一种方式,不光可读,而且可写,因此,让s2中的第4个字符变为字母x,即:

s2[4] = 'x';
cout << s2 << endl;

 运行结果如下:

3.2 string类对象的遍历:

为了遍历对象,首先需要知道其长度。对于获取string类对象的长度,可以采用size函数完成,具体使用方法如下:
 

for (int i = 0; i < s2.size(); i++)
	{
		cout << s2[i] << ' ';
	}

运行结果如下:

通过给出的上述函数,可以完成对于s2的逆置。代码如下 :

int begin = 0; 
	int end = s2.size() - 1;
	while (begin < end)
	{
		int tmp = s2[begin];
		s2[begin] = s2[end];
		s2[end] = tmp;

		begin++;
		end--;
	}
	for (int i = 0; i < s2.size(); i++)
	{
		cout << s2[i] << ' ';
	}

运行结果如下:

在上一篇关于模板的文章中C++(10)——模板-CSDN博客就以交换函数Swap来对函数模板进行说明。在string中,提供了交换函数Swap的实现,即:

因此,可以通过此函数,来对上述代码完成简化,即:

int begin = 0; 
	int end = s2.size() - 1;
	while (begin < end)
	{
		swap(s2[begin], s2[end]);
		begin++;
		end--;
	}
	for (int i = 0; i < s2.size(); i++)
	{
		cout << s2[i] << ' ';
	}

运行结果如下:

4. 迭代器:

对于string类对象的访问,除了使用operator[],还可以使用迭代器进行访问。即:

具体使用方法如下:

cout << "迭代器访问测试" << endl;
	string::iterator i = s2.begin();

	while (i != s2.end())
	{
		cout << *i << " ";
		++i;
	}
	return 0;
}

运行结果如下:


 

 对于一个const类型的对象,即:

const string s4("hello world");

	string::const_iterator i1 = s4.begin();

	while (i1 != s4.end())
	{
		cout << *i1 << " ";
		++i1;
	}

运行结果为:

对于逆置迭代器,对于const和非const对象的访问方式如下:

cout << "非const对象逆置访问" << endl;
	string s5("HELLO WORLD");

	string::reverse_iterator i2 = s5.rbegin();

	while (i2 != s5.rend())
	{
		cout << *i2 << ' ';
		++i2;

	}
	cout << endl;
	cout << "const类型对象逆置访问" << endl;

	string::const_reverse_iterator i3 = s4.rbegin();

	while (i3 != s4.rend())
	{
		cout << *i3 << ' ';
		++i3;
	}

 运行效果如下:

5.string类对象的容量操作:

5.1 size和capacity:

对于如何获取一个string类对象的存储容量,可以用关键字capacity完成,即:

string s1;  //创建一个空的string类对象
	string s2("hello world");

	cout << s1.capacity() << endl;
	cout << s2.capacity() << endl;

运行结果如下:

不难发现,capacity的大小只和该类型能够存储多少内容有关,与对象中是否有内容无关。

对于前面用到的size,是用于反应这个对象中字符串的长度的。与对象中是否有内容有关。例如:

	cout << s1.size() << endl;
	cout << s2.size() << endl;

 运行结果如下:

5.2 reserve和resize:

reserve,resize主要用于改变string类对象的大小。对于reserve,假如在知道了需要扩容的大小后,可以对string类对象进行扩容,例如:

string s1;  //创建一个空的string类对象


	cout << "进行扩容前" << endl;
	cout << s1.capacity() << endl;
	cout << s1.size() << endl;
	s1.reserve(500);
	cout << "进行扩容后" << endl;
	cout << s1.capacity() << endl;
	cout << s1.size() << endl;

运行结果为:

需要注意,使用reserve进行扩容时,扩容后的大小并不一定等于预设的值,通常会大于这个值。

当使用reservestring类对象进行缩容操作时,例如:

	string s2("hello world");
	
	cout << " 进行缩容前" << endl;
	cout << s2.capacity() << endl;
	cout << s2 << endl;
	cout << s2.size() << endl;
	cout << " 进行缩容后" << endl;
	s2.reserve(5);
	cout << s2.capacity() << endl;
	cout << s2 << endl;
	cout << s2.size() << endl;

运行结果为:

通过上述结果,不难看出,使用reservestring类对象进行缩容时,并不能缩小到这个对象的最小容量。同时,reserve只能改变空间,并不能影响对象中的内容。这一点可以从缩容前后的size值看出。

对于resize,与reserve只影响空间不影响内容不同,resize对于二者都会影响。例如:

string s2("hello world");  //创建一个空的string类对象


cout << "进行扩容前" << endl;
cout << s2.capacity() << endl;
cout << s2.size() << endl;
s2.resize(500);
cout << "进行扩容后" << endl;
cout << s2.capacity() << endl;
cout << s2.size() << endl;

通过打印前后的结果可以看到,利用resize进行扩容后,对象的capacity,size都发生了变化。

对于其中内容的改变,可以通过监视窗口进行查看:

进行扩容前, s2中的内容如下图所示:

进行扩容后,发现,此时扩充的部分的值全部为\0 :

对于使用resize进行扩容时,也可以自定义后面的扩充部分的内容,例如:

cout << "进行扩容前" << endl;
	cout << s2.capacity() << endl;
	cout << s2.size() << endl;
	s2.resize(500,'x');
	cout << "进行扩容后" << endl;
	cout << s2.capacity() << endl;
	cout << s2.size() << endl;

运行结果为:

当利用resize进行缩容时,此时有两种情况:
resize(n)size<n < capacity,即缩容后的空间小于对象的最小存储,但是大于对象中字符串的长度:

cout << "进行缩容前" << endl;
	cout << s2.capacity() << endl;
	cout << s2.size() << endl;
	s2.resize(12);
	cout << "进行缩容后" << endl;
	cout << s2.capacity() << endl;
	cout << s2.size() << endl;
	

运行结果如下:

 从结果可以看出,不影响capacity的值。但是会改变size的值,并且还会用\0进行填充。

当 n < size <capacity时,即:

cout << "进行缩容前" << endl;
	cout << s2.capacity() << endl;
	cout << s2.size() << endl;
	s2.resize(8);
	cout << "进行缩容后" << endl;
	cout << s2.capacity() << endl;
	cout << s2.size() << endl;
	

运行结果如下:

通过结果可以看出,此时的resize对于capacity,size都有影响,并且会删除对象中的内容,这一点可以从监视窗口进行确认:

 

6. 对于对象中内容的修改:

6.1 push_back:

使用push_back可以在已有对象后方插入单个字符,例如:

string s4("hello");

	s4.push_back('a');
	cout << s4 << endl;

打印结果如下:


 

6.2 append:

对于append的使用有多种方式,例如:


string& append (const string& str);

string& append (const char* s)

对于第一种,使用方法如下:

string s4("hello");
const string& s5  = "world";
s4.append(s5);
cout << s4 << endl;

运行结果如下:

6.3 operator +=:

使用方式同样有许多种:


string& operator+= (const string& str);

string& operator+= (const char* s);

string& operator+= (char c);

例如:

s6 += 'a';
	cout << s6 << endl;
	s6 += "ABCDE";
	cout << s6 << endl;
	s6 += s5;
	cout << s6 << endl;

运行结果如下:

 

7. 勘误:

  由于个人能力有限,书中难免出现汉字拼写错误、代码意义解释错误、内容逻辑以及理解错误等不同类型的错误。首先感谢各位大佬能花掉自己宝贵的时间阅读此文章,愿大佬们斧正,发现错误可以通过私信联系,本人不胜感激。

  • 21
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个简单的C++文字RPG游戏的示例代码,其中包括了基本的角色、战斗和物品系统: ```c++ #include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; // 角色类 class Character { public: string name; int hp; int atk; int def; int gold; Character(string n, int h, int a, int d, int g) { name = n; hp = h; atk = a; def = d; gold = g; } // 攻击函数 void attack(Character& other) { int damage = atk - other.def; if (damage < 0) { damage = 0; } other.hp -= damage; cout << name << "攻击了" << other.name << ",造成了" << damage << "点伤害。" << endl; } // 是否死亡 bool isDead() { return hp <= 0; } }; // 物品类 class Item { public: string name; int price; int hp; int atk; int def; Item(string n, int p, int h, int a, int d) { name = n; price = p; hp = h; atk = a; def = d; } }; // 商店类 class Shop { public: Item items[3]; Shop() { items[0] = Item("草药", 10, 20, 0, 0); items[1] = Item("铁剑", 50, 0, 10, 0); items[2] = Item("铁甲", 100, 0, 0, 10); } // 显示商店物品 void showItems() { cout << "欢迎光临!以下是本店的物品:" << endl; for (int i = 0; i < 3; i++) { cout << i + 1 << ". " << items[i].name << " - " << items[i].price << "金币" << endl; } } // 购买物品 bool buy(Character& c, int choice) { if (c.gold < items[choice - 1].price) { cout << "金币不足,法购买!" << endl; return false; } c.gold -= items[choice - 1].price; c.hp += items[choice - 1].hp; c.atk += items[choice - 1].atk; c.def += items[choice - 1].def; cout << "购买成功!" << endl; return true; } }; // 战斗函数 void battle(Character& player, Character& enemy) { cout << "你遇到了一只" << enemy.name << ",准备战斗!" << endl; while (!player.isDead() && !enemy.isDead()) { player.attack(enemy); if (enemy.isDead()) { cout << enemy.name << "被你打败了!" << endl; player.gold += enemy.gold; return; } enemy.attack(player); if (player.isDead()) { cout << "你被" << enemy.name << "打败了!" << endl; return; } } } int main() { srand(time(NULL)); // 初始化随机数种子 // 初始化角色和商店 Character player("勇者", 100, 10, 5, 50); Character enemies[3] = { Character("史莱姆", 30, 5, 2, 10), Character("骷髅兵", 50, 10, 5, 20), Character("巨龙", 100, 20, 10, 50) }; Shop shop; // 游戏循环 while (true) { cout << "你的状态 - HP:" << player.hp << " ATK:" << player.atk << " DEF:" << player.def << " 金币:" << player.gold << endl; cout << "请选择操作:" << endl; cout << "1. 进入商店" << endl; cout << "2. 进行战斗" << endl; cout << "3. 离开游戏" << endl; int choice; cin >> choice; switch (choice) { case 1: shop.showItems(); cout << "请选择要购买的物品(输入编号):" << endl; cin >> choice; shop.buy(player, choice); break; case 2: battle(player, enemies[rand() % 3]); break; case 3: cout << "游戏结束,欢迎再次光临!" << endl; return 0; default: cout << "无效的操作!" << endl; break; } } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

起床写代码啦!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值