c++程序初写(3个适合初学者的小程序)

最近刚开始学c++,主要通过看书,视频以及学习网上大牛的笔记,以下程序是在网上和视频学习中,学习别人的程序后自己动手编写的,可能会有一些重复的地方,望谅解。

学习总结:

1.编写测试案例时可以使用不同的命名空间namespace,这样可以避免使用同一个变量,并且易于操作修改程序,看着也很舒服

2.在每个命名空间前添加此命名空间中所需要的头文件(不存在重复使用,c++内部能避免重复使用),不用都添加到顶部,这样可以清晰明了的知道此命名空间中使用的头文件


test1

简单运用namespace命名空间 class类 构造函数 析构函数 构造函数重载的小程序


#include<iostream>
using namespace std;

namespace Test1
{
	class test
	{
		int *high, *weight, testnum;
	public:
		test(int, int);//---构造函数
		~test();//----析构函数,释放内存
		int area() {
			return *high * *weight;
		}
		test(int);//---构造函数重载
		int testapp() {
			return 10 * testnum;
		}


	};
	test::test(int x, int y)
	{
		high = new int;//---new的用法相当于mallac,分配内存
		weight = new int;
		*high = x;
		*weight = y;
	}
	test::~test()
	{
		delete high;//---释放内存
		delete weight;

	}
	test::test(int x)
	{

		testnum = x;
	}
}
test2
运用友元函数的例程

类girl把类boy的成员函数disp作为友元函数,输出类girl中私有成员的年龄和身高

namespace Test2
{
	class boy
	{
		int age;
		int high;
	public:
		boy(int,int);
		friend class girl;//--友元声明
		void disp(girl &);//boy成员函数,做成girl类中友元函数实现
	};
	boy::boy(int x, int y)
	{
		age = x;
		high = y;
	}
	class girl
	{
		int age;
		int high;
	public:
		girl(int ,int);
		 friend void boy::disp(girl &);//这里把boy类的成员函数作为友元函数
	};
	girl::girl(int x,int y)
	{
		age = x;
		high = y;

	}
	void boy::disp(girl &x)//--boy类成员函数实现,友元函数定义
	{
		cout << "boy's age is :" << age << "  " << "boy's high is :" << high << endl;
		cout << "girl;s age is :" << x.age << "  " << "girl's high is :" << x.high << endl;

	}


}
test3
学习vector基本用法+排序+二分查找+普通查找
程序:运用vector容器存储100000个随机数值,计算存储花费时间,

并通过普通查找和二分查找法查找(需要先排序)容器中是否含有12345这个数,比较两种查找方法所花时间

#include<vector>//---vector容器头文件
#include<ctime>//---时钟函数头文件
#include <algorithm>//---sort头文件
#include<cstdio>//--c库函数头文件
#include<cstdlib>//--sbort
#define SIZE 100000


namespace Test3
{
	int compareints(const void *a, const void * b)
	{
		return (*(long*)a - *(long*)b);
	}
	void test()
	{
		cout << "testing begin ..........\n";
	clock_t Time_start = clock();
	vector<long> buf;
		for (long i = 0; i < SIZE; i++)
		{
			try//----防止容器分配不到内存
			{
				buf.push_back(rand());
			}
			catch (exception& p)
			{
				cout << "i = " << i << "problem is " << p.what() << endl;//---曾有i = 136216567problem is bad allocation
				abort();//--异常退出
			}
			
		}
		cout << "存储容器花费时间为:" << clock() - Time_start << "ms"<<endl;
		cout << "vector size:" << buf.size() << endl;
		cout << "vector data:" << buf.data() << endl;
		cout << "vector max_size:" << buf.max_size() << endl;
		cout << "vector front:" << buf.front() << endl;
		cout << "vector capacity:" << buf.capacity() << endl;//---目前容器的容量
		
		Time_start = clock();
	long key = 12345;
	auto position = ::find(buf.begin(), buf.end(), key);//--作用域符号在这里表示全局模板函数,使用auto自动类型判断
		cout << typeid(position).name() << endl;//-查看position的类型
		cout << "查找数值12345花费时间为:" << clock() - Time_start << "ms" << endl;
		if (position != buf.end())
		{
			cout << "find the num is " << *position << endl;
		}
		else
			cout << "not find the num " << endl;


		Time_start = clock();
		sort(buf.begin(), buf.end());//--用sort排序
		cout << "排序花费时间为:" << clock() - Time_start << "ms" << endl;
		Time_start = clock();
	long *pitem = (long *)bsearch(&key, buf.data(), buf.size(), sizeof(int), compareints);//--二分查找法查找
		cout << "二分查找数值12345花费时间为:" << clock() - Time_start <<"ms"<< endl;
		if (pitem != NULL)
			cout << "find the num :" << *pitem << endl;
		else
			cout << "not find the num" << endl;
	}
}

主函数块

int main()
{
	using namespace Test1;//---使用命名空间Test1
	test test00(3, 4);//---调用构造函数
	test test11(5, 6);
	cout << "Test1**************Test1\n";
	cout << "第一个area:" << test00.area()<<endl;
	cout << "第二个area:" << test11.area() << endl;
	test test22(4);
	cout << "testapp为:" << test22.testapp() << endl;
	cout << "\n";cout << "\n";

	using namespace Test2;
	cout << "Test2**************Test2\n";
	int a = 22, b = 170, c = 20, d = 160;
	boy boy1(a,b);
	girl girl1(c,d);
	boy1.disp(girl1);
	cout << "\n";cout << "\n";

	using namespace Test3;
	cout << "Test3**************Test3\n";
	Test3::test();

	return 0;
}

运行结果

猜你喜欢

转载自blog.csdn.net/zhangxiafll/article/details/79804839