1.C/C++基础

C/C++基础题135.二进制中1的个数 Hello: Person person = new Person(); person.Name = “xueyubin”; person.WeChat = “18309212110”; person.HeaderPhoto=“戴眼镜、黑眼圈、格子衫、牛仔裤、双肩包”; person.Sex = “男”; String major[] = { ‘C’,“C++”, “Linux”,“MySQL” }; person.IWantSay(“关心足够大的问题,注重考察成本的增长趋势!”) 加入专栏,变强!!!!. 阅读详情

1.交换a、b的值

//交换a,b的值
#include<stdio.h>

void swap1(int& a, int& b)
{
int temp = a;  //使用局部变量temp完成交换
a = b;
b = temp;
printf("a=%d, b=%d\n",a,b);
}


void swap2(int& a, int& b)
{
a = a + b;  //使用加减运算完成交换,运算时可能溢出会导致数据
b = a - b;
a = a - b;
printf("a=%d, b=%d\n",a,b);
}


void swap3(int& a, int& b)
{
a = a^b;   //a=a^b;
b = a^b;   //b=a^b=a^b^b=a^0=a;
a = a^b;   //a=a^b^a=b^0=b


printf("a=%d, b=%d\n",a,b);
/*
使用异或运算完成交换,原理:1.x^x=0,  2.x^0=x, 3.具有结合性,可以交换位置
当a=b时此方法不适用。本方法为这三种方法中最优的。
*/
}


int main()
{
int a1 = 1, b1 = 2;
int a2 = 1, b2 = 2; 
int a3 = 1, b3 = 2;
swap1(a1,b1);
swap2(a2,b2);
swap3(a3,b3);


return 0;
}

a=2, b=1
a=2, b=1
a=2, b=1



2.宏定义的使用


//宏定义的使用
//1.用#define实现宏并求最大值和最小值
#include<stdio.h>
#define MAX(x,y) (((x)>(y))?(x):(y))
#define MIN(x,y) (((x)<(y))?(x):(y))
//使用#define实现宏并求输入参数的平方
#define SQR(x) ((x)*(x))
int main()
{
int a = 1, b = 2;
int min, max, aa;
min = MIN(a, b);
max = MAX(a,b);
aa = SQR(b);
printf("min=%d\nmax=%d\n", min,max);
printf("%d*%d=%d\n",b,b, aa);
return 0;
}

min=1
max=2
2*2=4



3.const的使用

找出程序中的错误

//const的使用。找出程序中的错误
#include<stdio.h>
int main()
{
const int x = 1;
int b = 10;
int c = 20;


const int* a1 = &b;
int* const a2 = &b;
const int* const a3 = &b;


x = 2; //错误,变量x是整形常量,因此不能改变x的值


a1 = &c;  //正确,const在int*的左侧,它是用修饰指针所指向的常量,即指针指向常量。
        //因此,这一句把a1指向变量c是允许的,因为修改的是指针a1本身,而不是指针指向的那个值
*a1 = 1; //错误,改变a1指向的内容是不允许的


a2 = &c; //错误,const在int*的右侧,它是用来修饰指针本身的,即指针本身为常量。
        //那么修改指针a2是不允许的
*a2 = 1; //正确,修改a2指向的内容是允许的。


a3 = &c; //错误,a3有两个const,表示不仅指针不能修改,并且其指向的内容也不能修改
*a3 = 1; //错误


return 0;
}



4.C++中const的作用

 (1).const用于定义常量
 (2).const修饰函数形式参数
 当输入参数为用户自定义类型和抽象数据类型时,应该将“值传递”改为“const&传递”,可以提高效率
  void fun(A a);
  void fun(A const &a)
  第一个效率低,函数体内产生A类型的临时对象用于复制参数a,临时对象的构造、复制、析构过程都将
  消耗时间。而第二个函数提高了效率,用“引用传递”不需要产生临时对象,节省了临时对象的构造、复制、
  析构过程消耗的时间。但光引用有可能改变a,所以加const
 (3).const修饰函数的返回值
 如给“指针传递”的函数返回值加const,则返回值不能被直接修改,且该返回值只能被赋值给加const修饰
 的同类型指针。
 const char* GetChar(void){};
 char *ch=GetChar();  //错误
 const char *ch=GetChar(); //正确 
 (4).const修饰类的成员函数(函数定义体)
 任何不会修改数据成员的函数都应用const修改,这样,当不小心修改了数据成员或调用了非const成员函数
 时,编译器都会报错。const修饰类的成员函数形式为:
 int GetCount(void) const;



7.C++类的静态成员示例
//#include<stdio.h>

#include<iostream>

using namespace::std;
class widget
{
public:
widget()
{
count++;
}
~widget()
{
--count;
}
static int num()
{
return count;
}
private:
static int count;
};

int widget::count = 0;

int main()
{
widget x, y;      //两个实例,输出2
cout << "The Num.is" << widget::num() << endl;
if (widget::num()>1)
{
widget x, y, z;  //又定义了3个实例,输出5,if语句结束后,这3个实例销毁
cout << "The Num.is " << widget::num() << endl;
}
widget z; //又定义一个实例,输出3
cout << "The Num is" << widget::num() << endl;


return 0;
}

//类widget有一个静态成员count和一个静态方法num。我们知道,类中的静态成员或方法不属于类的实例,
//而属于类本身并在所有类的实例间共享。在调用他们时应该用类名加上操作符“::”来引用。

//类widget的构造方法里把静态成员count的值加1,在类的析构方法里把静态成员count的值减1。也就是说,
//静态成员count的值表示类widget实例的个数。


The Num.is2
The Num.is 5
The Num is3



8.使用sizeof计算普通变量所占空间大小

#include<iostream>
using namespace::std;

int main()
{
char str[] = "hello";
char *p = str;
int n = 10;
cout << "sizeof(str)= " << sizeof(str) << endl;  //6,strlen("hello")+1,数组中有一个元素保存字符串结束符
cout << "sizeof(p)= " << sizeof(p) << endl;  //4,在32位WinNT平台下,指针和int都是4个字节
cout << "sizeof(n)= " << sizeof(n) << endl;  //4
return 0;
}


sizeof(str)= 6
sizeof(p)= 4
sizeof(n)= 4


9.使用sizeof计算类对象所占空间大小

#include<iostream>
using namespace::std;
class A
{
public:
int i;
};

class B
{
public:
char ch;
};

class C
{
public:
int i;
short j;
};

class D
{
public:
int i;
short j;
char ch;
};

class E
{
public:
int i;
int ii;
short j;
char ch;
char chr;
};

class F
{
public:
int i;
int ii;
int iii;
short j;
char ch;
char chr;
};

class S1
{
char c1;
int i;
char c2;
};
class S2
{
int i;
char c1;
char c2;
};
int main()
{
cout << "sizeof(int) = " << sizeof(int) << endl;   //int占4个字节
    cout<< "sizeof(short) = " << sizeof(short) << endl; //short占2个字节
cout << "sizeof(char) = " << sizeof(char) << endl;  //char 占1个字节
cout << endl;


cout << "sizeof(A) = " << sizeof(A) << endl; //4
cout << "sizeof(B) = " << sizeof(B) << endl; //1
cout << "sizeof(C) = " << sizeof(C) << endl; //4+2+2(补齐)=8
cout << "sizeof(D) = " << sizeof(D) << endl; //4+2+1+1(补齐)=8
cout << "sizeof(E) = " << sizeof(E) << endl;  //4+4+2+1+1=12,不需要补
cout << "sizeof(F) = " << sizeof(F) << endl;  //4+4+4+2+1+1=16,不需要补
cout << "sizeof(S1) = " << sizeof(S1) << endl;  // 1+3(补)+4+1+3(补)=12
cout << "sizeof(S2) = " << sizeof(S2) << endl;  // 4+1+1+2(补)=8
return 0;
}


sizeof(int) = 4
sizeof(short) = 2
sizeof(char) = 1

sizeof(A) = 4
sizeof(B) = 1
sizeof(C) = 8
sizeof(D) = 8
sizeof(E) = 12
sizeof(F) = 16
sizeof(S1) = 12
sizeof(S2) = 8



10.使用sizeof计算含有虚函数的类对象的空间大小

#include<iostream>
using namespace::std;

class Base
{
public:
Base(int x) :a(x)
{
}
void print()
{
cout << "base" << endl;
}
private:
int a;
};

class Derived :public Base
{
public:
Derived(int x) :Base(x - 1), b(x)
{
}
void print()
{
cout << "derived" << endl;
}
private:
int b;
};

class A
{
public:
A(int x) :a(x)
{
}
virtual void print()
{
cout << "A" << endl;
}
private:
int a;
};

class B :public A
{
public:
B(int x) :A(x - 1), b(x)
{
}
virtual void print()
{
cout << "B" << endl;
}  
private:
int b;
};

int main()
{
Base obj1(1);
cout << "size of Base obj is " << sizeof(obj1) << endl;
Derived obj2(2);
cout << "size of Derived obj is " << sizeof(obj2) << endl;

A a(1);
cout << "size of A obj is " << sizeof(a) << endl;
B b(2);
cout << "size of B obj is " << sizeof(b) << endl;
return 0;
}
//对于Base类来说,它占用内存大小为sizeof(int),等于4,print()函数不占内存。
//对于Derived类来说,比Base类多一个整型成员,因而多4个字节,一共是8个字节。
//对于A类来说,由于它含有虚函数,因此占用的内存除了一个整型变量之外,还包括
  //一个隐含的虚表指针成员,一共是8个字节
//对于B类来说,比A类多一个整型成员,因而多4个字节,一共是12个字节。
//注意:普通函数不占用内存,只要有虚函数,就会占用一个指针大小的内存,原因是系统多用了
//一个指针维护这个类的虚函数表,并且注意,无论这个类中再有多少个虚函数,都不会再影响类的大小了。

size of Base obj is 4
size of Derived obj is 8
size of A obj is 8
size of B obj is 12



虚拟继承是多重继承中特有的概念。虚拟基类是为解决多重继承而出现的。如:类D继承自类B1、B2,而类B1、B2都继承自类A,因此在类D中两次出现类A中的变量和函数。为了节省内存空间,可以将B1、B2对A的继承定义为虚拟继承,而A就成了虚拟基类。实现的代码如下:

class A

class B1:public virtual A;

class B2:public virtual A;

class D:public B1,public B2;

虚拟继承在一般的应用中很少用到,所以也往往被忽视,这也主要是因为在C++中,多重继承是不推荐的,也并不常用,而一旦离开了多重继承,虚拟继承就完全失去了存在的必要因为这样只会降低效率和占用更多的空间。


11.使用sizeof计算虚拟继承的类对象的空间大小

#include<iostream>
using namespace std;

class A
{
};

class B
{
};

class C :public A, public B
{
};

class D :virtual public A
{
};

class E :virtual public A, virtual public B
{
};

class F
{
public:
int a;
static int b;
};

int F::b = 10;

int main()
{
cout << "sizeof(A) = " << sizeof(A) << endl;
cout << "sizeof(B) = " << sizeof(B) << endl;
cout << "sizeof(C) = " << sizeof(C) << endl;
cout << "sizeof(D) = " << sizeof(D) << endl;
cout << "sizeof(E) = " << sizeof(E) << endl;
cout << "sizeof(F) = " << sizeof(F) << endl;

return 0;
}
//A类,由于A类是空类,编译器会安插一个char给空类,用来标记它的每一个对象。因此其大小为1个字节
//B类,和A类一样,也是1
//C类多重继承自A和B,其大小仍然为1个字节
//D类虚继承自A,编译器为该类安插一个指向父类的指针,指针大小为4。由于此类有了指针,编译器就不会
  //安插一个char了,因此其大小是4个字节。
//E类虚继承自A并且也虚继承自B,因此它有指向父类A的指针和指向父类B的指针,加起来大小为8个字节。
//F类含有一个静态成员变量,这个静态成员的空间不在类的实例中,而是像全局变量一样在静态存储区中
  //被每一个类的实例共享,因此F类的大小就是int的大小,为4个字节。


sizeof(A) = 1
sizeof(B) = 1
sizeof(C) = 1
sizeof(D) = 4
sizeof(E) = 8
sizeof(F) = 4



12.使用sizeof计算联合体的大小

#include<iostream>
using namespace std;
union u
{
double a;
int b;
};

union u2
{
char a[13];
int b;
};

union u3
{
char a[13];
char b;
};

int main()
{
cout << "sizeof(u) = " << sizeof(u) << endl;
cout << "sizeof(u2) = " << sizeof(u2) << endl;
cout << "sizeof(u3) = " << sizeof(u3) << endl;

return 0;
}
//联合体的大小取决于它所有的成员中占用空间最大的一个成员的大小。并且对于复合数据类型,
//如union、struct、class的对齐方式为成员中最大的成员对齐方式。
//对于u来说,8,大小就是最大的double类型成员a了,即sizeof(u)=sizeof(a)=8
//对于u2来说,16,最大的空间是char[13]类型的数组。这里需要注意,由于它的另一个成员int b的存在,u2的对齐方式
  //变为4,也就是说,u2的大小必须在4的对齐上,所以占用的空间变为最接近13的对齐,即16
//对于u3来说,13,最大的空间是char[13]类型数组,即sizeof(u3)=13


sizeof(u) = 8
sizeof(u2) = 16
sizeof(u3) = 13



13.#pragma pack的作用
//对齐是可以更改的,使用#pragma pack(x)可以改变编译器的对齐方式

#include<iostream>
using namespace std;
#pragma pack(1)  //设置对齐方式为1
 
struct test
{
char c;
short s1;
short s2;
int i;
};
int main()
{
cout << "sizeof(test) = " << sizeof(test) << endl;  //sizeof(test)=1+2+2+4=9
return 0;
}


sizeof(test) = 9


14.为什么要引入内联函数
//引入内联函数的主要目的是,用它替代C语言中表达式形式的宏定义来解决程序中函数调用的效率问题。


15.为什么不把所有的函数都定义成内联函数
//(1)如果函数体内的代码比较长,使用内联将导致内存消耗代价较高。
//(2)如果函数体内出现循环,那么执行函数体内代码的时间要比函数调用的开销大。


16.内联函数与宏有什么区别
//(1).内联函数在编译时展开,宏在预编译时展开
//(2).在编译的时候,内联函数可以直接被镶嵌到目标代码中,而宏只是一个简单的文本替换
//(3).内联函数可以完成诸如类型检测、语句是否正确等编译功能,宏就不具有这样的功能。
//(4).宏不是函数,inline函数是函数
//(5).宏在定义时要小心处理宏参数(一般情况是把参数用括号括起来),否则容易出现二义性。而内联函数定义时不会出现二义性


C/C++基础题128.0到n-1中缺失的数字 Hello: Person person = new Person(); person.Name = “xueyubin”; person.WeChat = “18309212110”; person.HeaderPhoto=“戴眼镜、黑眼圈、格子衫、牛仔裤、双肩包”; person.Sex = “男”; String major[] = { ‘C’,“C++”, “Linux”,“MySQL” }; person.IWantSay(“关心足够大的问题,注重考察成本的增长趋势!”) 加入专栏,变强!!!!. 阅读详情

相关推荐

保姆级教程:用STM32H743+TJA1042T玩转FDCAN,CubeMX配置1M/5M双速率通信(附完整代码)

本文提供STM32H743与TJA1042T实现FDCAN双速率通信的保姆级教程,详细讲解CubeMX配置1M/5M双速率通信的全过程,包括硬件连接、时钟树配置、过滤器设置及中断管理,并附完整代码示例,助力开发者快速掌握FDCAN高速通信技术。

weixin_30698297的博客 485

Sizeof的计算看内存分配

本文记录了有关sizeof的一些计算,主要有下面的四种情况:(如有错误,敬请留言) 使用sizeof()计算普通变量所占用的内存空间 sizeof计算类对象所占用空间的大小-用到了字节对齐 sixeof计算含有虚函数的类对象的空间大小 sizeof计算虚拟继承的类对象的空间大小 使用sizeof()计算普通变量所占用的内存空间 #include <iostream&g...

weixin_30679823的博客 197

nbiot和2g_浅谈NBIOT

一、什么是NBIOT?1、概念窄带物联网(Narrow Band Internet of Things, NB-IoT),NB-IoT构建于蜂窝网络,只消耗大约180KHz的带宽,使用License频段,可采取带内、保护带或独立载波等三种部署方式,与现有网络共存。可直接部署于GSM网络、UMTS网络或LTE网络,以降低部署成本、实现平滑升级。2、NBIOT的特点和目标(1)超强覆盖:相对GPRS来...

weixin_39534100的博客 1706

Scala中交换两个变量的值的3中实现方法

需求 已知有两个Int类型的变量a和b, 初始化值分别为10和20, 请写代码实现变量a和变量b的值的交换. 即最终结果为: a=20, b=10. 注意: 不允许直接写a=20, b=10这种代码. *方式一: 通过算术运算符实现. //定义两个Int类型的变量a和b, 初始化值分别为10和20 var a = 10 var b = 20 //将变量a和b的计算结果赋值给变量a a = a...

qq_36382679的博客 1632

【Agent】多智能体框架MetaGPT入门教程

创建一个 Action 类的子类 SimpleWriteCode,子类中定义好 prompt,并解析返回结果。在 MetaGPT 中,

酒酿小圆子呀~ 3172

Halcon窗体开发避坑指南:C#自定义控件实现7大图像交互功能

本文详细介绍了如何利用C#封装自定义Halcon控件,以解决原生HWindowControl功能不足的问题。通过实战代码,展示了如何实现图像按比例显示、鼠标拖动缩放、灰度值读取、图像保存、截图及图形叠加等七大核心交互功能,并重点剖析了内存管理、坐标转换等开发避坑要点,帮助开发者构建稳定可复用的机器视觉组件库。

weixin_29251131的博客 304

欢迎来到我的博客世界!

今天我终于拥有了自己的Blog! 转载于:https://www.cnblogs.com/RIVERSPIRIT/archive/2005/11/23/283206.html

weixin_30588827的博客 172

深入解析Flex NOC总线驱动的实时带宽与延迟优化策略

本文深入解析了Flex NOC总线驱动的核心价值与优化策略。通过解读`/proc/noc/real_time`中的实时带宽与延迟数据,并结合`sysfs`接口进行动态QoS调优,可以有效解决芯片内部数据拥堵问题。文章以智能摄像头和算力盒子的实际案例,详细说明了如何通过优先级调整、带宽限制和参数优化,提升系统在复杂任务下的实时性与稳定性。

weixin_29289821的博客 292

chr(10),chr(9)的用法

chr(10)表示换行chr(9)表示打出一个TAB键的空格!VBscriptalert "a"&chr(9)&"b"&chr(10)&"c"&chr(9)&chr(10)&"d" VBscriptalert "a"&chr(9)&"b"&chr(10)&"c"&chr(9)&chr(10)&"d"

小强Ⅰ号的专栏 2116

模板 - 树上问题(树的直径、动态查询树的直径、树的重心)

整理的算法模板合集: ACM模板 目录一、树的直径树形DP两次DFS / BFS(找到直径的两个端点)二、动态修改树的边权并求每个时刻的直径(线段树)三、树的重心 一、树的直径 树的直径满足如下性质: 若有多条直径,则所有的直径之间皆有公共点。 直径的两端一定是叶子。 树中距离某一直径端点最远的点,至少有一个是该直径的另一个端点。 对于树上任意一个点,与之距离最远的一个点,至少有一个直径的端点。 给你一个无权无向的树。编写程序以输出该树中最长路径(从一个节点到另一个节点)的长度。在这

繁凡さん的博客 1689

使用 Simulink 搭建一个地震信号反射系数反演与滤波联合仿真的系统模型

《Simulink地震信号处理仿真指南》摘要:本文详细介绍了使用Simulink进行地震信号反射系数反演与滤波联合仿真的完整流程。从创建模型、生成含噪合成地震记录(包含子波卷积与随机反射系数),到设计滤波模块(带通/小波/自适应滤波)和实现反演算法(Wiener-Hopf方程/RLS方法),最后通过可视化模块分析结果。教程涵盖理论准备、7个实施步骤、优化建议及实际应用场景,为地震勘探、地质调查等领域提供标准化的信号处理仿真方案,特别适合需要掌握地震信号反演与实时滤波技术的工程人员学习。

amy_mhd的博客 117

C/C++基础学习代码(1)

/* *编写程序由从标准输入的设备读入的元素数据建立一个int型vector对象, *然后动态创建一个与该vector对象大小一致的数组, *把vector对象的所有元素复制给新数组。 */ #include &amp;amp;amp;lt;iostream&amp;amp;amp;gt; #include &amp;amp;amp;lt;vector&amp;amp;amp;gt; using namespace std; int main() { vector

walkerkalr的专栏 7104

【银河麒麟V10】【桌面】【服务器】系统启动问题

1、如果之前运行好好的,重启后系统报错,一般80%是因为fstab的问题,回想下之前有没有添加过外接存储,修改过/etc/fstab,如果是的话一般是因为存储异常(比如网络存储无法连接,本地存储路径变化)导致系统开机自检失败,导致无法正常启动; 处理方式:进入单用户(单用户进入的几种方式上面有专门文章介绍),修改/etc/fstab,将新增的内容暂时注释掉,然后重启系统,能正常进入系统后再分析失败原因。 ...

桂安俊@KylinOS 3万+

C/C++ 基础算法1

C/C++ 基础算法1 链表中倒数第k个节点   输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。   例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 1、2、3、4、5、6。这个链表的倒数第 3 个节点是值为 4 的节点。 给定一个链表: 1->2->3->4->5, 和 k = 2. 返回链表 4->5. /** * Definition for singly-linked list.

田螺姑娘的博客 1665

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

C++知识——1、C/C++程序基础

1、写出代码输出的题可能会遇到: 1)y==z:判断; 2)y | z:转为二进制,按位或运算; 3)y &amp;amp;amp; z:转为二进制,按位与运算; 4)“::”是“域操作符”,如 int value=0; { ::value=2; //此处的value本是局部变量,但有::,所以value成为全局变量。 } 5)注意值的传递性,如 int main(void) { int i...

李骁睿写技术的地方 276

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【菜鸟C/C++学习之路】1.1牛客网C/C++方向基础课程

秋招找工作发现自己真的还是太菜了,开启自己的学习之路。打算记录一下自己的学习之路,也当是个C/C++学笔记吧。 1.1 这节里面讲的是C语言的接口,但是没有说windows系统怎么从控制台传递参数,提供一下我的做法 先在visual c++编译器里面输入好代码 #include<stdio.h> int main(int argc,const char *argv[]) { ...

YLT0218的博客 508

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

C/C++基础1

1.1   C\C++语言基础 C\C++语言是国际上广泛流行的计算机高级语言。绝大多数硬件开发,均使用C/C++语言,Arduino也不例外。使用Arduino,需要有一定的C\C++基础,由于篇幅有限,本书仅对C\C++语言基础进行简单的介绍。此后章节中我们还会穿插介绍一些特殊用法及编程技巧。 1.1    1.1.1  数据类型 在C\C++语言程序中,对所有的数据都必须指定其数据类...

one_little_white的博客 249

1、C/C++基础学习

C语言部分 1.指针辨析 int (*p)[5]和int *p[5] 前者是一个指针,它指向一个含有5个元素的数组。后者是一个数组,它的长度为5,数组中每一个元素指向一个整型变量。 int *f( int i, int j)和 int (*p)( int i ,int j) 前者是返回指针的函数,它是一个函数的声明,后者是指向函数的指针,它定义了一个指针。

yuxikuo_1的专栏 992

第1章C/C++程序基础

作为程序员,你在求职时,公司会询问你的项目经验,例如你做过什么类型的项目、担任的是何种角色,以及做项目时如何与他人沟通,等等。除此之外,当然还要考查你的编程能力。这里包括你的编程风格,以及你对于赋值语句、递增语句、类型转换、数据交换等程序设计基本概念的理解。因此,最好在考试之前复习这些程序设计的基本概念,并且要特别重视那些比较细致的考点问题。本章列出了一些涉及C/C++程序设计基本概念的考题,希望读者在读完后能有所收获。

chenyijun的专栏 928

《C和C++程序员面试秘笈》第1章 C/C++程序基础

1. 一般赋值语句 & 按位与 对应的具体的数 && 逻辑与 0或1 || 逻辑或 0或1 三目运算符 有时可以代替条件判断 if/else/else if 2. C++域操作符 同名时:局部变量优先调用 C++中通过域操作符“::”来直接操作全局变量 3. i++和++i的区别 ...

liangwenhao1108的博客 323

【C/C++】1 C语言基础 -- hello world

【序】 em...dota生活太颓废...我决定回馈一波社会...重新开始写博客。有不正确或不准确的表述or你看到的任何觉得可以优化的代码欢迎指出,谢谢。此系列所有代码见本人Github:https://github.com/YidaoXianren/CPP_Yidao_Blog 不废话,马上开始吧骚年们。 个人认为C和C++是十分相通的,甚至于C++是建立在C之上又将它发扬光大的一门语言...

u013541048的博客 282

c/c++理论基础知识选择填空题库1

已知x,y为整型,z为实型,ch为字符型,则下列表达式中正确的是 【B】 A. z=(x+y)++ B. y=ch+x C. x+y=z D. y=z%x 执行程序段 int a = -1, b = 4, k; k = ( ++ a < 0) && !(b – <= 0); cout << a << ‘ ’ << b << ‘ ’ << k; 后的输出结果是 【B】 A. 0 4 1 B. 0 4 0 C...

一只可爱的小猴子的博客 3579

c/c++教程 - 总集1. c基础语法

一、代码的注释 // 单行注释 /* ... ... 多行注释 ... ... */ #include <iostream> using namespace std; /* 多行注释 main 主函数 */ int main() { cout << "Hello World!!" << endl; // 单行注释,打印输出Hello world!! system("pause"); return 0; } cout

Mark_md的博客 457

C/C++_lesson1_C++语法基础

C/C++_lesson1_C++语法基础

yaoming168的专栏 613

C/C++从0到1系统精讲 项目开发综合基础课

通过上述步骤,你可以从零开始学习C/C++,并逐步掌握其核心概念和高级特性。选择一个合适的项目进行实践,不仅能加深对所学知识的理解,还能积累实际开发经验。

小白技术的博客 943

c/c++易错知识点整理1(c/c++程序基础)

一、c/c++程序基础 1.在main函数里局部变量引用优先,c++中可以通过域操作符“::”来直接操作全局变量; 2.i=8;  printf("%d",-i++);    先打印-i的值,在i++ ,打印-8,而i此时i=9; 3.在内建数据类型的情况,i++和++i效率没有区别; 4.良好的条件比较语句: (1)bool flag; if(flag);

JeremyZJM的博客 730
上一篇: 15.MATLAB因子分析
下一篇: 2.C/C++引用和指针
技术提高效率
博客等级 码龄10年 1664粉丝 119原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术提高效率

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址