c++ 基本使用
1 枚举
enum ShapeType
{
circle,
square,
rectangle
}; int main() { ShapeType shape = circle; switch(shape)
{
case circle:
cout<<"ShapeType.circle"<<endl;
break;
case square:
cout<<"ShapeType.square"<<endl;
break;
case rectangle:
cout<<"ShapeType.rectangle"<<endl;
break;
default:
cout<<"Unknown!"<<endl;
} return ;
}; 输出:
ShapeType.circle
2. const_cast, static_cast
const int i = ;
/*
error C2440: 'const_cast' : cannot convert from 'const int' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
转换是一个合法的标准转换,可以通过隐式执行,或使用static_cast、C风格的转换、函数式转换进行
*/
//int j = const_cast<int>(i);
/* 从 const int --> int, 下面4中都可以,唯独const_cast常量转换方式不行,只是因为它是一个合法的标准转换,用简单的方式已能处理还用不到高级的const_cast常量转换 */
int j = i; //隐式转换
int h = static_cast<int>(i); //static_cast静态转换
int k = (int)i; //C风格转换
int t = int(i); //函数式转换
/*
const int * --> int * 使用const_cast常量转换
*/
int* s = const_cast<int *>(&i);
/*
error C2440: 'static_cast' : cannot convert from 'const int *' to 'int *'
Conversion loses qualifiers
*/
int* w = static_cast<int *>(&i);
//long* l = const_cast<long *>(&i); // error C2440: 'const_cast' : cannot convert from 'const int *' to 'long *'
const_cast的用法:
返回该常量对应的变量 = const_cast<该常量所属的类型>(常量)
const int * --> int * 使用 const_cast
const int i = ;
/* error C2440: 'initializing' : cannot convert from 'const int *' to 'int *' */
//int* p = &i;
int* p = const_cast<int *>(&i);
const int --> int 是一个合法的标准转换,const int直接就可以隐含转换到int, eg. int i = 5;
3 union联合体
union Packed
{
char i;
short j;
int k;
long l;
float f;
double d; //该union的大小为double,8个字节
}; //分号是必须的,用于结束该union的定义 int main() { cout<<"sizeof(Packed) = "<<sizeof(Packed)<<endl; /*
* 联合体,用同一个变量,处理不同的数据类型
*/
Packed p; p.i = 'C';
cout<<p.i<<endl; p.d = 3.14159;
cout<<p.d<<endl; //此时联合体变量p保存的是d成员的值 return ;
};
4 数组
int a[10];
创建了10个存储单元连续的int型变量,但每一个变量都没有单独的标识符。相反,它们都集结在名字a下
int a[]; //数组名a的类型信息为int *
const int *p = a; //指针变量p的类型信息为int const * cout<<"a typeinfo: "<<typeid(a).name()<<endl; //int *
cout<<"p typeinfo: "<<typeid(p).name()<<endl; //int const * cout<<"sizeof(int) = "<<sizeof(int)<<endl; cout<<"a = "<<(long) a<<endl; //数组名的值 for (int i=; i<; i++)
cout<<"&a["<<i<<"] = "<<(long) &a[i]<<endl;

int型量占用4个字节,数组a占用的内存单元从176到212+4,共40个字节
数组名a的值,是第一元素a[0]的地址, a的值 与 &a[0] 相等
想给一个函数传递数组
1)声明一个数组作为函数参数
2)声明一个指向数组元素的指针
void func1(int a[], int size)
{
for (int i=; i<size; i++)
a[i] = i*i -i;
} void func2(int* a, int size)
{
for (int i=; i<size; i++)
a[i] = i*i +i;
} void print(int a[], string name, int size)
{
for (int i=; i<size; i++)
cout << name << "[" << i << "] = " << a[i] <<endl;
cout<<"--------------------------"<<endl;
} int main() { int a[]; //数组未初始化,输出的是一些无意义的值 -- 把数组a作为参数传给print函数,通过指针直接在同一块内容单元上操作,修改有效
print(a, "a", ); //初始化数组,并输出
func1(a, );
print(a, "a", ); //修改数组元素的值,并输出
func2(a, );
print(a, "a", ); return ;
};

命令行参数字符指针数组
命令行中每一个用空格分隔的字符串都被转换为一个单独的命令行参数
int main(int argc, char* argv[])
{ cout<<"argc = "<<argc<<endl; //命令行参数的个数 for (int i=; i<argc; i++)
cout<< "argv[" << i << "] = " << argv[i] <<endl; return ;
};

字符指针数组,即指向字符型的指针的数组,数组中的每个元素都是指向字符型的指针

5 函数指针
void func(int x)
{
cout<<"func("<< x <<") called ... "<<endl;
} int main(int argc, char* argv[])
{ void (*fp)(int); //定义函数指针fp, 该函数有1个int参数,无返回值
fp = func;
(*fp)(); //func(2) called ... void (*fp2)(int) = func; //定义函数指针fp, 并把函数func的地址赋给它
(*fp2)(); //func(3) called ... return ;
};
随机推荐
- 26718汉字,gbk是23940个汉字,gb18030有76556个汉字
1 a 厑 吖 呵 啊 嗄 嬶 腌 錒 锕 阿 仰 卬 岇 昂 昻 枊 盎 肮 腌 軮 醠 雵 骯 侒 俺 儑 匎 匼 厂 厈 唵 啽 垵 埯 堓 媕 安 屵 岸 峎 峖 广 庵 按 揞 晻 暗 案 ...
- 【python小随笔】celery周期任务(简单原理)
1:目录结构 |--celery_task |--celery.py # 执行任务的main函数 |--task_one # 第一个任务 |--task_two # 第2个任务 . . . . |-- ...
- Leetcode674.Longest Continuous Increasing Subsequence最长连续递增序列
给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...
- PHP学习(运算符)
PHP运算符一般分为算术运算符.赋值运算符.比较运算符.三元运算符.逻辑运算符.字符串连接运算符.错误控制运算符. 算术运算符 主要是用于进行算术运算的,例如:加法运算.减法运算.乘法运算.除法运算 ...
- 一个基于Asterisk构建的VOIP应用软件:Elastix介绍
Elastix 是一种应用软件,它整合了适用于那些基于 Asterisk 的 PBX 的最好工具,并将它们集成为单一的.易用的接口.同时,它增加了自己的工具集,以及允许创建第三方模块来使 Elasti ...
- iOS开发 底层抛析运行循环—— RunLoop
http://blog.csdn.net/zc639143029/article/details/50012527 一.RunLoop基本概念 概念:程序的运行循环,通俗的来说就是跑圈. 1. 基本作 ...
- iOS9 CASpringAnimation 弹簧动画详解
http://blog.csdn.net/zhao18933/article/details/47110469 1. CASpringAnimation iOS9才引入的动画类,它继承于CABaseA ...
- MaxCompute助力小影短视频走向全球化
数字时代,中国已经成为世界互联网的中心,小影(海外版称作为VivaVideo,后简称VivaVideo)作为国内首批短视频出海企业,借助统一的云计算平台快速实现全球业务的线上部署,已经让每一行代码都获 ...
- 【JZOJ4884】【NOIP2016提高A组集训第12场11.10】图的半径
题目描述 mhy12345学习了树的直径,于是开始研究图的半径,具体来说,我们需要在图中选定一个地方作为中心,其中这个中心有可能在路径上. 而这个中心的选址需要能够使得所有节点达到这个中心的最短路里面 ...
- 数据分析1:安装tushare安装包
1. 2. 3.重点内容