C++——String

本文介绍了C++中的String容器,重点讲述了它的特性,如内存管理安全、丰富的API等。详细讨论了String的构造函数、赋值操作、字符存取、拼接、比较、查找与替换、插入与删除以及子串截取等功能,并对比了存取字符时[]和at方法的不同之处。
摘要由CSDN通过智能技术生成

C++之String容器

1、特性:

1)和char*对比:char * 是一个指针,String是一个类,String封装了char *,是他的容器

2)String中封装了很多方法:

operate+=(加法拼接)、compare(>=,==,<=,<,>)、length/size(长度)、insert、erase、substr(截取长度)、find/rfind(查找最后一次出现的位置)、replace

3)不用担心内存释放与越界

4)可以通过 c_str()方法 **把string转char *** s_str()方法 把char *转string

2、常用API

1)构造函数
string();//创建一个空的字符串 例如: string str;      
string(const string& str);//使用一个string对象初始化另一个string对象
string(const char* s);//使用字符串s初始化
string(int n, char c);//使用n个字符c初始化 

//例子:

//默认构造函数
string s1;

//拷贝构造函数
string s2(s1);
string s2 = s1;

//带参数构造函数
char* str = "itcast";
string s3(str);
string s4(10, 'a');
2)基本赋值操作
//第一种 直接用=
#include<iostream>
#include<string>
using namespace std;
int main()
{
   
    string s1="ahahaha";
    string s2;
    s2=s1;
    cout<<s2<<" ";
    return 0;
}
//  第二种  用assign()方法
#include<iostream>
using namespace std;

int main()
{
   
    string str1("hello");
    string str2;
    string str3;
    string str4;
    string str5;
    //====================================
    str2.assign(str1);  //把字符串str1赋值给当前字符串
    str3.assign("World", 4);   //把字符串World的前4个字符赋给当前的字符串
    str4.assign(str1, 2, 3);   //把str1从2开始的3个字符赋值给当前字符串
    str5.assign(10, 'c');      //用10个字符c赋值给当前字符串
    //====================================
    cout<<str1<<endl;
    cout<<str2<<endl;
    cout<<str3<<endl;
    cout<<str4<<endl;
    cout<<str5<<endl;
    //====================================
    system("pause");
    return 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、付费专栏及课程。

余额充值