C++基础 (一)

本文深入探讨了C++编程的基础知识与高级技巧,包括枚举类型、共同体、内存管理、字符串操作、文件读写、时间延迟等核心内容。通过实际代码示例,讲解了变量初始化、数据类型转换、条件判断、循环结构、函数调用、异常处理等方面的知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<climits>
#include<cstdio>
#include<iostream>
#include<vector>
#include<array>
#include<string>
#include<ctime>

using namespace std;

struct MyStruct
{
	int type;
	double num;
	union   //匿名共同体
	{
		int id_int;
		float id_float;
	};
};

enum Number { one, two, three, four};  //枚举的范围  0~7(2^3-1)

int main()
{
	printf("%d\n", INT_MAX);
	printf("int:%d, long long:%d\n", sizeof(int), sizeof(long long));

	int test1{};
	int test2 = {};

	int test3 = 42;
	cout << "dec:" << test3 << endl;
	cout << oct;
	cout << "oct:" << test3 << endl;
	cout << hex;
	cout << "hex:" << test3 << endl;
	cout << dec;

	cout.put('M');
	cout.put('\n');

	char ch = '\n';
	cout << "'\\n': " << int(ch) << endl;

	int password;
	cout << "Please Enter Password:________\b\b\b\b\b\b\b\b";
	//cin >> password;
	cout << endl;

	cout << "g\u00E2" << endl;

	//宽字符类型
	wchar_t wch = L'P';
	wcout << "wchar_t: " << wch << endl;

	vector<vector<int>> t_vec;
	vector<int> t_vec1 = { 1,2,3,4,5 };
	auto pv = t_vec.begin();

	t_vec.push_back(t_vec1);

	cout << t_vec[0].size() << endl;

	char* str = new char[8];
	str[0] = 'a';
	str[1] = 'b';
	str[2] = 'c';
	str[3] = 'd';
	str[4] = '\0';
	cout << str << endl;

	//两种方式赋值字符数组
	char str1[] = "HelloWorld!";
	char str2[12] = "HelloWorld!";
	char str3[12];
	cout << "Input String: ";
	(cin >> str3).get();  //cin以空格、换行符、制表符作为字符串的结束
	//cin.getline(str3, 12);   //cin.getline()  遇到换行符或达到长度停止读取 丢弃换行符
	//cin.get(str3, 12);    //cin.get()  读取一行,但换行符仍在输入序列中,下一个输入为\n
	//    cin.get(str3,12);
	//    cin.get();   //获取换行符  or  cin.get(str3, 12).get();
	//    cin.get(str4, 12);


	cout << "strlen: " << strlen(str3) << endl;
	cout << "sizeof: " << sizeof(str3) << endl;

	string str4("aaaaa");
	string str5("bbbbb");
	int len_str2 = strlen(str2);  //都是c风格字符串的函数 strlen strcpy strcat
	/*strcpy_s(str3, str2);
	strcat_s(str3, str2);*/
	int len_str4 = str4.size();

	cout << "getline(cin,str5): ";
	getline(cin, str5);
	cout << str5 << endl;

	MyStruct ms;
	ms.id_int = 10;

	//int* pt1;	//容易出问题
	//*pt1 = 100;

	int* pt2 = new int;
	*pt2 = 100;

	vector<int> vc(5);  //比数组安全,但效率低
	array<int, 5> ai;   //效率与数组一样,比数组安全(在栈中)

	int x = 1;
	cout << (x > 3) << endl;
	cout.setf(ios_base::boolalpha);
	cout << (x > 3) << endl;

	cout << x << ++x << x << ++x << endl;  //3 3 3 3 
	cout << x << x++ << ++x << x++ << endl;   // 6 5 6 3

	/*char word[] = "word";
	cout << word == "word" << endl;*/  //ERROR
	string word1 = "";
	bool flag = word1 == "word";

	cout << "Wait 1s......" << endl;
	int delay = 1 * CLOCKS_PER_SEC;
	clock_t start = clock();
	while (clock() - start < delay)
		;

	cout << "Input A String(End with #): ";
	cin >> ch;  
	while (ch != '#')
	{
		cout << ch;
		cin >> ch;		//不会读入空格和换行
	}
	cout << endl;

	cout << "Input A String(End with #): ";
	cin.get(ch);
	while (ch != '#')
	{
		cout << ch;
		cin.get(ch);
	}
	cout << endl;

	cout << "Input A String(End with EOF): ";
	cin.get(ch);
	while (cin.fail() == false)  //test of EOF  (or cin.eof())  or while(cin.get(ch))
	{
		cout << ch;
		cin.get(ch);
	}
	cout << endl;

	cout << "continue input: ";
	cin.clear();  //EOF之后不会再输入,要用cin.clean()清楚标记
	cin.get(ch);			
	cout << ch << endl;



	return 0;
}
#include<cctype>
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
	char ch = 'a';
	cout << isalpha(ch) << endl;
	cout << ispunct(ch) << endl;
	cout << islower(ch) << endl;
	cout << char(toupper(ch)) << endl;
	ch = ',';
	cout << ispunct(ch) << endl;
	cout << isdigit(ch) << endl;
	ch = '\n'; 
	cout << isspace(ch) << endl;  // \n \t 

	int type = 1;
	switch (type)
	{
	case 1:cout << 1 << endl;break;
	case 2:cout << 2 << endl;break;
	default:cout << "no" << endl;
	}

	ofstream myFile;
	myFile.open("test.txt");
	myFile << "Yes, this is my first file!\n";
	cout.precision(2);
	myFile.precision(2);
	double d = 112.1009;
	myFile << d;
	myFile.close();

	ifstream inFile;
	inFile.open("test.txt");
	if (!inFile.is_open())
	{
		exit(0);
	}
	char line[100];
	//inFile.getline(line, 100);
	inFile.get(line, 100);
	float f;
	inFile >> f;
	cout << "EOF:" << inFile.eof() << endl;
	inFile.close();
	cout << line << endl;
	cout << f << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值