C++——string类(1)

### string是C++中的一种类,在标准库中的;可以直接对字符串进行一系列操作。

string类类型的构造

1、无参构造:

string();

定义string对象的时候不给值,这个string类的对象里面没有字符;

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

 这里的字符串直接能够输出是因为重载了string类的输出函数。

2、给一个字符串去初始化:

string(const char* s);

这个对象就是这个给定的字符串。

int main()
{
	string s1("Initial String");
	cout << s1 << endl;
	return 0;
}

3、拷贝已存在的string去初始化:

string(const string& str);

int main()
{
	string s0("Initial String");
	string s1(s0);//拷贝
	cout << s1 << endl;
	return 0;
}

4、指定拷贝的开始去拷贝一个已存在的string对象的 len 个字符去初始化:

string(const string& str , size_t pos , size_t len = npos);

 npos的值是-1,这个缺省值是无符号整形的最大值;当拷贝的时候用到这个缺省值,表示拷贝到字符串的最后一个字符;

int main()
{
	string s0("Initial String");
	//从下标为0的位置的字符开始拷贝7个字符
	string s1(s0, 0, 7);
	cout << s1 << endl;
	cout << s1.size() << endl;

	//结束位置超过字符串的长度,拷贝只拷贝到最后一个有效字符
	string s2(s0, 7, 100);
	cout << s2 << endl;

	//不给定结束位置,使用缺省值npos,也就是拷贝到字符串最后一个有效字符
	string s3(s0, 0);
	cout << s3 << endl;
	return 0;
}

 5、用一个字符串(非 string 类)的前 n 个去初始化:

string(const char* , size_t n);

int main()
{
	//用这个给定字符串的前n个去初始化;
	string s1("Initial String", 7);
	cout << s1 << endl;
	return 0;
}

6、用n个字符c去初始化:

string(size_t n ,char c); 

int main()
{
	string s1(10, 'x');
	cout << s1 <<endl;
	return 0;
}

7、利用迭代器,从一个string对象的开始begin到结束end位置的字符去按顺序去初始化:

template <class InputIterator>

        string (InputIterator first ,InputIterator last);  

int main()
{
	string s0("Initial String");
	string s1( s0.begin(), s0.end());
	cout << s1 << endl;
}

string类类型对象的遍历:

1、operato[]重载[],下标访问string类类型对象中的某一个字符:

 可以遍历const型的普通类型的string对象:

int main()
{
	string s0("Test String");
	for (size_t i = 0; i < s0.size(); i++)
	{
		cout << s0[i];
	}
	return 0;
}

2、范围for:

基本性质:

范围for是一种迭代方式,这种方式只需要指定迭代的每个类型和迭代的范围就可以遍历这个数组或者字符串;

自动迭代,自动取数据,自动判断结束;

for之后括号内包括两部分,由 : 分开,前面的一部分表示类型,后面的一部分表示要迭代的对象。

使用范围:

数组或者容器对象(例如string类类型的对象)。

底层:

范围for的容器遍历底层就是迭代器。

int main()
{
	string s1("Test String");
	for (char ch : s1)
	{
		cout << ch ;
	}
	cout << endl;
	int arr[] = { 1,2,3,4,5,6 };
	for (int i : arr)
	{
		cout << i << " ";
	}
	return 0;
}

3、迭代器遍历: 

int main()
{
	string s1("Test String");
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it;
		it++;
	}
	return 0;
}

首先看到迭代器中的begin和end

 迭代器iterator包含在string类中,使用方式如上代码,表示指向这个字符串的第一个字符;可以想象成指针理解;

end指向字符串的最后一个有效字符的下一个位置;

用const限制的迭代器可以接收非const类型的string对象(权限缩小);非const限制的迭代器在遍历时可以改变非const限制的string对象。


 迭代器:

1、begin和end:上面介绍了;

2、cbegin和cend:即const限制的迭代器,对应的begin和end前面要加上c,并且在定义迭代器时,要在iterator前面加上const_;

const限制的迭代器的本身可被改变,但是指向的内容不能被改变;

代码示例:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s0("String Test");

	const string s1(s0);
	string::const_iterator it = s1.cbegin();
	while (it != s1.cend())
	{
		cout << *it;
		it++;
	}
	return 0;
}

3、rbegin和rend:这个是反向迭代,rbegin指向最后一个有效字符,rend指向第一个有效字符的前一个位置:

int main()
{
	string s1("String Test");
	string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit;
		rit++;
	}
	return 0;
}

4、crbegin和crend:反向迭代并且迭代器指向的内容不能被修改:

int main()
{
	const string s1("Iterator Test");
	string::const_reverse_iterator crit = s1.crbegin();
	while (crit != s1.crend())
	{
		cout << *crit;
		crit++;
	}
	return 0;
}

 

反例:指向的内容不能被改变:


auto关键字:

auto关键字是C++11引入的语法;它可以定义一个对象,并且在编译阶段根据等式右边的明确类型的对象推导自己定义的对象;

auto可以作为函数的返回值,但是不能成为函数的参数;

auto不能直接用来声明数组;

当等式右边是一个指针类型时,左边写auto和auto*是一样的;

auto声明引用类型时写成auto&;

auto一次声明多个数据时,这些数据必须是相同类型的,否则会编译出错。

代码示例:

int main()
{
	int a = 1;
	double b = 1.1;
	char* c = new char('x');
	int& d = a;

	auto i = a;
	auto j = b;
	auto k = c;//或者auto* k
	auto& n = d;
    
    //打印类型名字
	cout << typeid(i).name() << endl;
	cout << typeid(j).name() << endl;
	cout << typeid(k).name() << endl;
	cout << typeid(n).name() << endl;
	return 0;
}

不能声明数组:

不能同时声明多个类型的: 

  • 9
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的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; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值