C++实现explode

源码版

#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const vector<string> explode(const string &s,const char &c){
	string buff;
	vector<string> v;
	char tmp;
	for (int i=0;i<s.size();i++)
	{
		tmp = s[i];
		if (tmp!=c){
			buff+=tmp;
		}else {
			if(tmp==c && buff !=""){
				v.push_back(buff);
				buff = "";
			}
		}//endif
	}
	if (buff !="")
	{
		v.push_back(buff);
	}
	return v;
}
int main(){
	string str("the quick brown fox jumps over the lazy dog");
	vector<string> v = explode(str, ' ');
	for(int i = 0;i<v.size();i++)
	{
		cout << v[i] << endl;
	}
	return 0;
}

strtok函数

int main(){
	string str="the quick brown      fox jumps   over the lazy dog";
	char const delimeters[] = " ";
	char *ins = const_cast<char *>(str.data());
	char *tok = strtok(ins,delimeters);
	while(0!=tok){
		puts(tok);
		tok = strtok(NULL,delimeters);
	}
	return 0;
}
### C++ 面向对象烟花效果实现 为了创建一个基于C++的面向对象烟花效果程序,可以采用粒子系统的概念来模拟烟花爆炸的效果。通过定义多个类来表示不同的组件,如`Particle`, `Firework`, 和 `ParticleSystem`,能够有效地管理各个部分的行为。 #### 类结构概述 - **Particle(粒子)**: 表示单个火花或颗粒,具有位置、速度、加速度以及生命周期等属性。 - **Firework(烟花)**: 组合了大量粒子,并负责初始化这些粒子的位置和其他参数。 - **ParticleSystem(粒子系统)**: 控制整个场景中的所有烟花实例及其更新逻辑。 下面是一个简化版的例子: ```cpp #include <iostream> #include <vector> #include <cmath> class Particle { public: float x, y; float vx, vy; int life; Particle(float _x, float _y): x(_x), y(_y), vx((rand() % 200 - 100) / 100.f), vy((rand() % 200 - 100) / 100.f), life(rand() % 50 + 50) {} void update() { // 更新位置和减少生命值 x += vx * 0.1f; y -= vy * 0.1f; // 假设向上为正方向 vy -= 0.05f; // 加入重力影响 --life; } bool isDead() const {return life <= 0;} }; class Firework { private: std::vector<Particle> particles; public: Firework(float startX, float startY){ for (int i = 0; i < 100; ++i) particles.emplace_back(startX, startY); } void explode(){ for(auto& p : particles)p.update(); } void draw(/* Drawing context */){} bool allParticlesDead()const{ return std::all_of(particles.begin(),particles.end(), [](const auto& particle){return particle.isDead();}); } }; // 粒子系统用于管理和绘制所有的烟花 class ParticleSystem { private: std::vector<Firework*> fireworks; public: ~ParticleSystem(){ for(auto* fw : fireworks) delete fw; } void addFirework(Firework* firework){ fireworks.push_back(firework); } void runSimulation(/* Graphics Context */) { for(size_t i=0;i!=fireworks.size();++i){ if(!fireworks[i]->allParticlesDead()){ fireworks[i]->explode(); fireworks[i]->draw(/* ... */); } else { // 清除已经结束的生命体 delete fireworks[i]; fireworks.erase(fireworks.begin()+i--); } } // 添加新的烟花... } }; ``` 此代码片段展示了如何利用C++构建基本的面向对象架构来模拟烟花效果[^1]。请注意,在实际应用中还需要考虑更多细节,比如更复杂的物理模型、色彩变化、声音同步等功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值