C和C++的关系, namespace, struct , class

目录

1 关系

2 Namespace

3 struct

4 增强


1 关系

 

 

2 Namespace

 

#include <iostream>


// 命名空间的引入方式   
//引入命名空间的第一种方式
//第二种
using std::cout; //using 关键字 不是引入整个命名空间 而是引入命名空间一个变量
using std::endl; 


//第三种
using namespace  std; //using namespace 是引入整个命名空间

int aa;//是属于默认的全局作用域命名空间


//如何定义一个命名空间?
namespace namespaceA//定义一个命名空间 namespace是命名空间关键字类型, namespaceA 是命名空间的名字
{
	// namespace A 空间领域
	int a = 10;

	int b = 20;
}

namespace namespaceB
{
	int a = 20;

	namespace namespaceC
	{
		struct teacher
		{
			int id;
			char name[64];
		};
	}

	namespace namespaceD
	{
		struct teacher
		{

		};
	}
}



//使用自定义的命名空间
void test()
{
	//using namespaceA::a;//真个test()函数领域中所有的a 默认都是 namespaceA中的a
	//using namespace namespaceA; //引入整个namespaceA空间
	cout << "A::a = " << namespaceA::a << endl;
	cout << "B::a = " << namespaceB::a << endl;
	//cout << "a = " << a << endl;


	//创建一个struct teacher的变量
	//using namespace namespaceB::namespaceC;//把namepaceC中的所有定义的遍历都引入
	//using namespaceB::namespaceC::teacher;

	//namespaceB::namespaceC::teacher t;
	using namespace namespaceB::namespaceC;
	teacher t;

}

int main(void)
{
	cout << "hello world" << endl;


	//第一种使用命名空间变量的方式
	std::cout << "你好" << std::endl;

	test();

	return 0;
}

3 struct

 

c++对c的struct做了功能增强。

4 增强

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>

using namespace std;

struct teacher
{
	int id;
	char name[64];
};

//2 对全局变量的检测能力加强, 一个变量不管是声明,还是定义,只能出现一次
int g_val ; //全局变量
//int g_val = 10;// 右一个全局变量

//1 实用性的增强, 对于变量的定义的位置,可以随意,没有要求
int test1(void)
{
	int i = 0;

	for (i = 0; i < 10; i++)
	{

	}

	for (int i = 0; i < 10; i++) {
	}
	
	return 0;
}

//2 
void test2()
{
	//teacher t1; //C++中 在使用struct 时候不需要再将 struct 写进来
}

//3 

void f(int i)
{
	cout << "i = " << i << endl;
}

//4 bool类型
void test3()
{
	bool flag = true; //就代表逻辑真
	flag = false;       //就代表逻辑假

	cout << "bool:sizeof() : " << sizeof(flag) << endl; //bool占一个字节。

	flag = true; // 为真 
	cout << "flag = " << flag << endl; //当bool类型为true 他的数值是1

	flag = false; //为假
	cout << "flag = " << flag << endl;//当bool类型为false 他的数值是0

	//flag = 10;
	cout << "flag = " << flag << endl;

	//flag = 100;
	cout << "flag = " << flag << endl;

	//flag = -100;
	cout << "flag = " << flag << endl; //不管给bool赋值什么样的非0数值,他是都是true 1 


	//flag = 0;
	cout << "flag = " << flag << endl; 

	//bool类型的取值,只有0 和1 


	if (flag == true) { //默认判断flag 是否为true

	}

}


void test5()
{
	int a = 10;
	int b = 20;

	(a < b ? a : b) = 30; //返回是a变量的别名 
	//a
	cout << "a = " << a << endl;
	(a < b ? 10 : b); //三木运算符 如果作为左值, 那么 返回的结果不能有常量
}

//c++对const的加强
void test6() {
	const int a = 10; //c++的const 确实对a起了保护的作用,不能通过指针的间接赋值概念a的值
							//
	//int const b; //const int , int const 是等价的

	int *p = (int*)&a; //当c++编译器 发现 对一个const的常量符号取地址,
	*p = 20;//C语言中可以通过指针的间接赋值改变const变量 
				//*p 是改变的临时的变量 而不是 常量a符号

	printf("a = %d\n", a);
}

#define A (3)
#define B (4)

void test7()
{
	const int a = 3;
	const int b = 4;  //此时说明 c++中的const 就是一个常量, 永远不会被改变
								//c++语言对const 的增强,将const 变量真正变成了常量

	int array[a + b] = { 0 };

	int array_2[A + B] = { 0 };  // define 和const 变量 在编译的阶段不一样, define是预处理器 const 是编译器

//#define ff (10)
	const int ff = 10; //const 是编译器出 完全符合编译器的逻辑判断和此法分析
}

void test8()
{
	//cout << "ff = " << ff << endl; //没有区域的划分
}

enum season {
	SPR ,
	SUM ,
	AUT  ,
	WIN
};

void test9()
{
	enum season s = SPR;

	s = SUM; //为了增加枚举的可读性, 
	s = AUT;
	s = WIN;

}

struct student
{
	int id;
	char name[64];
};

void change_stu(struct student *s)
{
	s ->id = 10;
}

void change_stu2(const struct student *s)
{
	//s->id = 10; //此时s所指向的区域是一个常量 不能够被修改
	struct student s2;
	s = &s2;
}

void change_stu3(struct student *const s)
{
	s->id = 10;
	//struct student s2;
	//s = &s2;  //s是一个常量指针
}

void change_stu4(const struct student *const s)
{
	//s->id = 10;
	//struct student s2;
	//s = &s2;  //s是一个常量指针
}

int g(int a, int b, int c)
{
	return 100;
}
//函数一定要有返回值, 如过函数没有返回值,也要加void




int main(void)
{
	//f(1, 2, 3); //error 对于函数传参的检测能力更加的严格,
	//g(1, 2, 3); //C_++编译器 对函数形参传递  函数返回值 做了严格的检查
	//test3();
	//test5();
	//test6();
	test7();
	test8();
	return 0;
}

 

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int g_val; //全局变量
int g_val = 10;// 右一个全局变量

typedef struct teacher
{
	int id;
	char name[64];
}teacher_t;

void f(int i)
{
	printf("i = %d\n", i);
}

g()
{
	return 10;
}

//3 C语言中的逻辑是与非 
void test3()
{
	int flag = 0; // C语言中 数值0表示为假
//	int flag = 1; // C语言中 数值非0表示真  C语言中是通过数值是否为0 来分辨逻辑真假的。
	 
}

//4 三目运算符的增强
void test4()
{
	int a = 10;
	int b = 20;

	int min_value = 0;


	//min_value = (a < b ? a : b);  
	//(a < b ? a : b) = 30;//返回的是一个数值, 10  , 10 = 30

	*(a < b ? &a : &b) = 30;

	//*(a < b ? &10 : &b) = 30; //10 =30

	
	printf("%d\n", a);
}


//5 c语言中的const 
void test5()
{
	const int a ;
	//int const b; //const int , int const 是等价的

	int *p = (int*)&a;
	*p = 20;//C语言中可以通过指针的间接赋值改变const变量 

	printf("a = %d\n", a);
}

#define A (3)
#define B (4)

void test6()
{
	int const a = 3;
	int const b = 4; //再次说明 c语言中的 const 并不是一个常量,是一个变量 加了只读属性。

	int array[A+ B] = { 0 }; //数组类型大小是固定, 将两个变量做长度,编译器无法确定长度
}

enum season {
	SPR = 0,
	SUM,
	AUT,
	WIN
};

void test7()
{
	enum season s = SPR;

	s = 0;
	s = 1;
	s = 2;  // C语言对枚举类型的赋值, 是可以通过枚举的值,直接赋值的。
	s = 136; //136  什么意思?

}

//1 实用性增强
int main(void)
{
	//struct teacher t1; //在C语言中如果使用一个struct 复杂类型,那么必须将struct 关键字写进来
	//teacher_t t2;
	//printf("%d\n", g_val);

	//f(1,2,3);//warning

	//int value = 0;
	//value = g(1, 2, 3, 4, 5);

	//printf("%d\n", value);
	
	//test4();
	//test5();
	test7();
	return 0;
}

 

都是在编译阶段处理的

 

通过制裁宏定义限定宏定义的作用域。

 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,使用结构体定义的vector是一种数据结构,它可以存储不同类型的元素,包括int、double、string等。结构体vector的定义需要注意,它应该是全局的,否则可能会出错。下面是一个简短的程序代码示例: ```c #include <stdio.h> #include <algorithm> #include <vector> #include <iostream> using namespace std; typedef struct rect { int id; int length; int width; // 对于向量元素是结构体的,可以在结构体内部定义比较函数,按照id、length和width升序排序。 bool operator< (const rect &a) const { if(id != a.id) return id < a.id; else { if(length != a.length) return length < a.length; else return width < a.width; } } } Rect; int main() { vector<Rect> vec; Rect rect; rect.id = 1; rect.length = 2; rect.width = 3; vec.push_back(rect); vector<Rect>::iterator it = vec.begin(); cout << (*it).id << ' ' << (*it).length << ' ' << (*it).width << endl; return 0; } ``` 此代码定义了一个名为rect的结构体,其中包含id、length和width三个成员变量。在main函数中,创建了一个vector容器vec,并向其中添加一个rect结构体。可以通过迭代器访问vector中的元素,并打印出id、length和width的值。 另外,如果想要强制释放STL Vector所占用的内存,可以使用swap方法。下面是一个ClearVector函数的模板示例: ```c template <class T> void ClearVector(vector<T>& v) { vector<T> vtTemp; vtTemp.swap(v); } ``` 可以使用类似以下的代码使用ClearVector函数: ```c vector<int> v; v.push_back(1); v.push_back(3); v.push_back(2); v.push_back(4); ClearVector(v); // 或者v.swap(vector<int>()); ``` 这样就可以强制释放v所占用的内存。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [c++中vector的用法详解](https://blog.csdn.net/qq_33263769/article/details/88714711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值