(c语法百题21卡塔 尔(英语:State of Qatar)四个人整数,语法几个人
知识点:
for语句,if语句灵活运用
剧情: 编制程序寻觅三个人整数 abcd 中级知识分子足下述关系的数: ( ab + cd )( ab + cd )
= abcd
输入表达:
不曾输入
出口表达:
出口abcd,从小到大,大器晚成行一个。
1 #include <stdio.h>
2 int main()
3 {
4 int i,a,b;
5 for(i=1000;i<10000;i++)
6 {
7 a=i/100;
8 b=i%100;
9 if((a+b)*(a+b)==i)
10 {
11 printf("%d\n",i);
12 }
13 }
14 return 0;
15 }
c++ auto_ptr智能指针,auto_ptr智能指针
c++ auto_ptr智能指针
该项目在头文件memory中,在程序的开明通过 #include<memory>
导入,接下去讲明该智能指针的作用和利用。
运用格局:
auto_ptr<type> ptr(new type()); 那是该指针的概念情势,此中type 是指针指向的门类,ptr 是该指针的称呼。
譬如该type 是int,具体定义如下:
auto_ptr<int> ptr(new int(4));
例如该type 是map<int,vector<int> >,具体定义如下:
auto_ptr<map<int,vector<int> > > ptr(new
map<int,vector<int> > ());
当然能够先定义,后赋值,如下所示:
auto_ptr<map<int,int> > ptr;
ptr = auto_ptr<map<int,int> >(new map<int,int>
());
功能1:保险贰个指标在有个别时间只可以被多少个该类别型的智能指针所指向,就是经常所说的目的全体权。
功用2:对针对的靶子活动释放的效劳,实际情况看如下代码。
代码片段后生可畏:
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;#define MAXN 20000000
class test_ptr
{
public:
map<int,int> *p;
test_ptr()
{
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; // 输出 创建次数
test_ptr * tmp = new test_ptr();
}
system("pause");
return 0;
}
在有些情形下,只怕大家就能够写出地点的代码来,通过运营会发掘有在内部存款和储蓄器溢出。对于部分经验老到的工程师只怕会作如下改写:
代码片段二:
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
map<int,int> *p;
test_ptr()
{
//p = auto_ptr<map<int,int> > (new map<int,int>());
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
~test_ptr()
{
delete p;
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl;
test_ptr * tmp = new test_ptr();
}
system("pause");
return 0;
}
在test_ptr
类中的析构函数中增多内部存款和储蓄器释放代码,可是在main函数中,定义的有的指针,当一些指针失效时并不会活动调用析构函数,在此种情景下也会以致内部存款和储蓄器泄漏难题。当然,假若分条析理的技术员能够在
test_ptr * tmp = new test_ptr() 后边加上一句 delete tmp
,那样也能够释放内部存款和储蓄器,不汇合世内存泄漏难题。然则在一些情形下,比较轻易漏写,为了缓慢解决此主题材料,auto_ptr
就能够发挥成效了。
代码片段三:
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
map<int,int> *p;
test_ptr()
{
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
~test_ptr()
{
delete p;
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; //输出创建次数
auto_ptr<test_ptr> tmp = auto_ptr<test_ptr> (new test_ptr());
}
system("pause");
return 0;
}
在main函数中,创建test_ptr类型指针时,该指针是auto_ptr
类型的智能指针,当智能指针失效时,会自动调用该类的析构函数。所以这种写法能够不再显得调用delete
语句了。可是该智能指针也只是保障调用类的析构函数,如若析构函数并不曾自由类中声称的变量,那该如何是好。
代码片段四:
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
//auto_ptr<map<int,int> > p;
map<int,int> *p;
test_ptr()
{
//p = auto_ptr<map<int,int> > (new map<int,int>());
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
/*
~test_ptr()
{
delete p;
}
*/
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; //输出创建次数
auto_ptr<test_ptr> tmp = auto_ptr<test_ptr> (new test_ptr());
}
system("pause");
return 0;
}
在这里种气象下,照旧会冒出内部存储器泄漏难题,为了消除该难点,对类中注脚的指针也是内需证明为auto_ptr类型。
代码片段五:
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
auto_ptr<map<int,int> > p;
test_ptr()
{
p = auto_ptr<map<int,int> > (new map<int,int>());
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; //输出创建次数
auto_ptr<test_ptr> tmp = auto_ptr<test_ptr> (new test_ptr());
}
system("pause");
return 0;
}
那般就无须呈现定义类的析构函数,不用在表面突显调用delete函数,当然假设尽早调用delete函数也是足以的,尽早获释内部存款和储蓄器也比该指针失效再自由好有的,这个正是为了堤防遗忘调用。
由此如上解析:能够吸取如下结论。
1 定义了智能指针,当智能指针失效时会自动调用类的析构函数。
2 在
类中定义的智能指针,不必在析构函数中显得的delete,当外界调用该类的析构函数时,会自行释放该智能指针指向的对象,释放内部存款和储蓄器。
3
假使类中定义的是智能指针,不过表面未有触发类中的析构函数调用,该智能指针指向的对象如故无法假释。
auto_ptr 智能指针的bug
auto_ptr 智能指针在c++ 11
规范中大器晚成度被放任,被抛弃的原故正是因为该bug。前面也关系过,叁个对象只好被三个智能指针所援用,那样就能够变成一个赋值难题。看如下代码
代码片段六:
1 #include <iostream>
2
3 #include <string.h>
4 #include <memory>
5 #include <set>
6
7 using namespace std;
8
9
10 #define MAXN 20000000
11
12 void pri(auto_ptr<set<int> > p)
13 {
14 set<int>::iterator ite = p->begin();
15 for(;ite!=p->end();ite++)
16 {
17 cout << *ite << endl;
18 }
19 }
20
21 int main(int argc,char *argv[])
22 {
23 auto_ptr<set<int> > ptr(new set<int> ());
24
25 for(int i = 0;i< 3;i++)
26 {
27 int a;
28 cin >> a;
29 ptr->insert(a);
30 }
31
32 pri(ptr);
33
34 pri(ptr);
35
36 system("pause");
37 return 0;
38 }
初看那代码没什么难点,可是运路程序会崩溃。那就是该智能指针最大的bug,
在程序32行 调用pri(ptr) ,程序到那并没什么难题,可是第三遍调用pri(ptr)
时程序就能够崩溃。原因正是前边讲过,一个目的智能被一个智能指针所针对,在率先次调用pri()函数时,为了确认保证那生龙活虎原则,当把ptr指针传入pri函数时,程序内部就把ptr置为空,所以到第三次调用时,就能产出崩溃的景色。对于这种情状的解决之道正是行使shared_ptr
指针(该指针的原理是透过援用流速計来促成的卡塔尔。
生机勃勃经要选取shared_ptr
智能指针,要求设置boost库,该库还包罗过多别样功能。风野趣的能够尝尝以下,该类中的智能指针依旧比较好用。也空中楼阁不少别的bug。
有的时候光再详尽介绍boost库中shared_ptr指针
auto_ptr智能指针,auto_ptr智能指针 c++
auto_ptr智能指针 该项目在头文件memory中,在前后相继的开明通过
#includememory 导入,接下去疏解该智能指…
<?xml version=”1.0″ encoding=”UTF-8″?>
<project name=”lb” default=”main” basedir=”.”>
<property file=”resouces.properties” />
<property name=”src.dir” value=”${basedir}/src”/>
<property name=”build.dir” value=”${basedir}/build”/>
<property name=”build.classes.dir”
value=”${build.dir}/classes”/>
<property name=”build.lib.dir” value=”${build.dir}/lib”/>
<property name=”lib.dir” value=”${basedir}/lib”/>
<property name=”dist.dir” value=”${basedir}/dist”/>
<property name=”web.dir” value=”${basedir}/web”/>
<property name=”deploy.dir”
value=”${tomcat.home}/webapps”></property>
<property name=”app” value=”lb”/>
<property name=”javac.target” value=”1.5″ />
<property name=”javac.source” value=”1.5″ />
<property name=”debug” value=”true” />
<property name=”deprecation” value=”false” />
<property name=”optimize” value=”true” />
<patternset id=”respository.files”>
<include
name=”org/springsource/spring-framework-3.0.5.RELEASE/dist/org.springframework.aop-3.0.5.RELEASE.jar”/>
<include
name=”org/springsource/spring-framework-3.0.5.RELEASE/dist/org.springframework.beans-3.0.5.RELEASE.jar”/>
<include
name=”org/springsource/spring-framework-3.0.5.RELEASE/dist/org.springframework.context-3.0.5.RELEASE.jar”/>
<include
name=”org/springsource/spring-framework-3.0.5.RELEASE/dist/org.springframework.core-3.0.5.RELEASE.jar”/>
<include
name=”org/springsource/spring-framework-3.0.5.RELEASE/dist/org.springframework.expression-3.0.5.RELEASE.jar”/>
<include
name=”org/springsource/spring-framework-3.0.5.RELEASE/dist/org.springframework.web-3.0.5.RELEASE.jar”/>
<include
name=”org/springsource/spring-framework-3.0.5.RELEASE/dist/org.springframework.web.servlet-3.0.5.RELEASE.jar”/>
<include name=”com/mchange/c3p0/0.9.1/c3p0-0.9.1.jar”/>
<include
name=”com/mysql/jdbc/5.1.21/mysql-connector-java-5.1.21-bin.jar”/>
<include name=”taglibs/standard/1.1.2/standard-1.1.2.jar”/>
<include name=”javax/servlet/jstl/1.1.2/jstl-1.1.2.jar”/>
</patternset>
<path id=”master-classpath”>
<fileset dir=”${lib.dir}”>
<include name=”*.jar”/>
</fileset>
<fileset dir=”${tomcat.home}/lib”>
<include name=”servlet*.jar”/>
</fileset>
<pathelement path=”${build.classes.dir}”/>
</path>
<target name=”main” depends=”init, getRespositoryFiles, compile,
war”>
</target>
<target name=”compile” depends=”init”>
<echo>Building…</echo>
<javac srcdir=”${src.dir}”
destdir=”${build.classes.dir}”
debug=”${debug}”
target=”${java.comp.level}”
source=”${java.comp.level}”
deprecation=”on”
optimize=”${optimize}”
failonerror=”true”
fork=”true”
executable=”${javac.exe}”
memoryinitialsize=”128m”
memorymaximumsize=”256m”>
<classpath refid=”master-classpath”></classpath>
</javac>
</target>
<target name=”init”>
<property environment=”env”></property>
<property name=”java.comp.level” value=”1.6″ />
<property name=”javac.exe” value=”${env.JAVA_HOME}/bin/javac.exe”
/>
</target>
<target name=”getRespositoryFiles” >
<copy todir=”${lib.dir}” verbose=”true”>
<fileset dir=”${basedir}/../../REPOSITORY/”>
<patternset refid=”respository.files”></patternset>
</fileset>
<mapper type=”flatten”></mapper>
</copy>
</target>
<target name=”clean” description=”Initialize build and dist
folders”>
<delete dir=”${build.dir}” />
<delete dir=”${dist.dir}” />
<delete dir=”${lib.dir}” />
<mkdir dir=”${build.dir}”/>
<mkdir dir=”${build.classes.dir}”/>
<mkdir dir=”${build.lib.dir}”/>
<mkdir dir=”${lib.dir}” />
</target>
<target name=”war”>
<echo>Archiving…</echo>
<mkdir dir=”${dist.dir}”/>
<copydir dest=”${build.lib.dir}” src=”${lib.dir}” />
<war destfile=”${dist.dir}/${app}.war”
webxml=”${web.dir}/WEB-INF/web.xml”>
<lib dir=”${build.lib.dir}”></lib>
<classes dir=”${build.classes.dir}”></classes>
<fileset dir=”${web.dir}”>
<include name=”**/*.*”/>
</fileset>
</war>
</target>
<target name=”copyWar” depends=”war”>
<echo>Deploy…</echo>
<copy todir=”${deploy.dir}”>
<fileset dir=”./${dist.dir}”>
<include name=”*.war”/>
</fileset>
</copy>
</target>