编程语言
首页 > 编程语言> > C++学习笔记

C++学习笔记

作者:互联网

C++学习笔记

 

数组的替代选项---模板类vector、array

void test11()
{
	unsigned short n = 10;
	const unsigned short m =10;
	vector<int> vi;
	vector<double>vd(n);
	array<int, 5> ai;
	//m必须是常量,而不能是变量,可以是一个数值10,或者const unsigned int类型的变量
	array<double, m> ad = { 1.0,2.0,3.0,4.0 };

	//-------------------------------------

	double a1[4] = { 1.1,2.2,3.3,4.4 };//静态数组,必须在定义时指定长度
	//a1[-1] = -1.0;//错误,索引值超出索引范围0-3
	
	//double a2[];//错误,静态数组必须在定义时指定长度
	//double a2[4]=a1;//错误,应该使用{}初始化聚合对象
	//a2 = a1;//错误,应该使用{}初始化聚合对象
	vector<double> vd1={ 2.22,3.25,5.65,6.74 };
	vector<double>vd2{ 2.22,3.25,5.65,6.74 };//可以省略等号进行便捷初始化
	vector<double> vd3(4);//不能直接初始化
	vd3= { 2.22,3.25,5.65,6.74 };
	vector<double>vd4(3);
	vd4[0] = 1.23;
	vd4[1] = 3.22;
	cout << "vd4[0]		" << vd4[0] << endl;
	cout << "vd4[1]		" << vd4[1] << endl;
	cout << "vd4[2]		" << vd4[2] << endl;
	cout << "-------------------------" << endl;
	vector<double> vd5 = vd4;
	cout << "&vd4[i]	" << "&vd5[i]" << endl;
	cout << &vd4[0]	<<"	" << &vd5[0] << endl;
	cout << &vd4[1] << "	" << &vd5[1] << endl;
	cout << &vd4[2] << "	" << &vd5[2] << endl;

	array<double, 4> arr1{ 5.0,6.0,7.0,8.0 };
	//array<double, 5> arr2=arr1;//错误,必须长度相等才可以赋值
	//array<long double, 4>arr3 = arr1;//错误,必须类型相同才可以赋值
	array<double, 4>arr4 = arr1;
	cout << "-------------------------" << endl;

	cout << "&arr4[i]	" << "&arr1[i]" << endl;
	cout << &arr4[0] << "	" << &arr1[0] << endl;
	cout << &arr4[1] << "	" << &arr1[1] << endl;
	cout << &arr4[2] << "	" << &arr1[2] << endl;
}

运行结果

vector之间可以直接=赋值,赋值后的vector是原vector的一个拷贝;

array之间可以直接=赋值,必须满足两个条件

1、array长度必须相等;

2、元素类型必须完全相同;

赋值后的array是原array的一个拷贝;

指向指针的指针

struct years
{
	int year;
	bool isLeepYear;

};
void test10()
{
	years y1, y2, y3;
	y1.year = 1998;			//成员运算符,用变量名访问成员
	years* py = &y2;
	py->year = 1999;		//间接成员运算符,用指针访问成员
	years arry[3];		//结构体数组
	arry[0].year = 2003; arry[1].year = 2004; arry[2].year = 2005;

	cout<<"arry->year		" << arry->year << endl;
	cout << "arry[1].year		" << arry[1].year << endl;
	cout << "arry->year		" << arry->year << endl;
	cout << "-------------------------"<<endl;
	years* arryp[3] = { &y1,&y2,&y3 };//结构体指针数组
	cout << "arryp[1]->year		" << arryp[1]->year << endl;
	//数组元素本身是指针(指向结构体),
	//而数组名也是指针(指向数组的第一个元素),
	//所以这里的数组名是指向指针的指针years**
	years** ppa = arryp;
	auto ppb = arryp;
	cout << "-------------------------" << endl;
	cout << "(*ppa)->year		" << (*ppa)->year << endl;
	cout << "(*(ppb + 1))->year		" << (*(ppb + 1))->year << endl;
}

运行结果

字符串测试

typedef unsigned int uint;

char* getname()
{
	char temp[80];
	cout << "Enter name (less than 80 characters)" << endl;
	cin >> temp;
	uint len = strlen(temp);
	cout << "len=" << len << endl;
	//通过一个小的内存空间来存放输入,以节省内存
	char* pc = new char[len + 1];
	//len表示字符串中字符的个数,而没有包含字符串结束标识"\0"
	//字符串所占字节数应当包含结束标识"\0",所以为len+1
	//strcpy已过时弃用,这里使用更加安全的strcpy_s
	//第一个参数表示拷贝的目标指针,char*类型
	//第二个参数表示需要拷贝的字节数,包含结束标识"\0",所以为len+1,
	// rsize_t类型,rsize_t类型属于size_t类型,而size_t类型属于unsigned int类型
	// rsize_t类型实际就是unsigned int类型,二者等效
	//第三个参数表示需要拷贝的数据源const char*类型
	strcpy_s(pc, len+1, temp);
	return pc;

}

void test9()
{
	char* name;
	name = getname();
	cout << "name=" << name << "	name at=" << (int*)name << endl;
	//只能释放new的资源
	delete name;
	name = getname();
	cout << "name=" << name << "	name at=" << (int*)name << endl;
	//只能释放new的资源
	delete name;
}

运行结果

打开文件是否成功的测试

void test2()
{
	ifstream instream;
	instream.open("in.dat");
	string s = instream.fail() ? "fail" : "succeed";
	cout <<"instream.fail()?	" << instream.fail() << endl;
	cout << "instream.fail()?	" << s << endl;
	if (!instream.fail())instream.close();
	instream.open("inFile.dat");
	s = instream.fail() ? "fail" : "succeed";
	cout << "instream.fail()?	" << instream.fail() << endl;
	cout << "instream.fail()?	" << s << endl;
	if (!instream.fail())instream.close();
}

运行结果

文件流测试

#include<iostream>
#include <fstream>
using namespace std;


void test1()
{
	ifstream inStream;
	ofstream outStream;
	inStream.open("inFile.dat");
	outStream.open("outFile.dat");
	int first, second, third;
	inStream >> first >> second >> third;
	cout << "first=" << first << "	second=" << second << "	third=" << third << endl;
	cout << "sum=" << first + second + third << endl;
	outStream << "first=" << first << "	second=" << second << "	third=" << third << endl;
	outStream << "sum=" << first + second + third << endl;
	inStream.close();
	outStream.close();
}

运行结果

标签:cout,笔记,years,学习,vector,C++,year,array,赋值
来源: https://www.cnblogs.com/zhangdezhang/p/16264787.html