【C/C++アカデミー】0814-参照高度、参照高度増加/auto自動変数自動タイプに応じてデータ/Bool/Enum/newdeleteグローバル/ビッグデータ乗算と構造体/関数テンプレートとauto/ワイド文字ローカライズ/inlineを作成
14996 ワード
参照の拡張、参照の拡張
自動変数自動タイプによるデータの作成
Bool
Enum
Cでは弱いタイプで、タイプチェックはしません.c++は強いタイプで、より厳しい要求があります.
Enum.c
Enum.cpp
新deleteグローバル
ビッグデータ乗算と構造体
関数テンプレートとauto自動変数
関数テンプレート
Autoと関数テンプレート
ワイド文字ローカライズ
inlineインライン関数
CCPPが異なる
C特徴
C++
#include<iostream>
#include<stdlib.h>
// int a[10]
// int (&ra)[10]
// int a[2][5]
// int (&ra)[2][5]
void main1()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int(&ra)[10](a);//
int i = 0;
for (auto data: ra)//C++11
{
data = i + 5;
std::cout << data << std::endl;
}
std::cout << a << ra << std::endl;
std::cout << &a << &ra << std::endl;
system("pause");
}
void main2()
{
int a[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int(&ra)[2][5](a);//
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
std::cout << " " << ra[i][j];
}
std::cout << "
";
}
std::cout << a << ra << std::endl;
std::cout << &a << &ra << std::endl;
system("pause");
}
int jia(int a, int b)
{
return a + b;
}
int jian(int a, int b)
{
return a - b;
}
void change(int(* & rp)(int,int))
{
rp = jian;
}
void main3()
{
int(* p)(int a, int b)(jia);
std::cout << p(1, 2) << std::endl;
//int(*&rp)(int a, int b)(p);//
//rp=jian;//()
change(p);
std::cout << p(1, 2) << std::endl;
system("pause");
}
int(*& changep(int (*&rp)(int,int)))(int, int)
{
rp = jian;
return rp;
}
void main4()
{
int(*p)(int a, int b)(jia);
std::cout << p(1, 2) << std::endl;
p = changep(p);
std::cout << p(1, 2) << std::endl;
system("pause");
}
void main5()
{
//int *p[4];
int a = 1, b = 2, c = 3;
int *px[3] = { &a, &b, &c };
//int && p [4] = {a,b,c };
//
}
struct mystr
{
int b;
double a;
char c;
// sizeof
void go()
{
std::cout << "123456789" << std::endl;
}
};
class MyClass
{
char & a;
char & b;
char & c;// , sizeof ,
// 4
};
void main6()
{
int num = 10;
int & rnum(num);
double db = 10.9;
double & rdb(db);//
std::cout << sizeof(rnum) << std::endl;
std::cout << sizeof(rdb) << std::endl;
std::cout << sizeof(MyClass) << std::endl;
system("pause");
}
int getdata(int && num)// , ,
{
std::cout << num << std::endl;
num += 10;
return num;
}
void main7()
{
int a = 5;
int b = 4;
std::cout << getdata(a+1) << std::endl;
system("pause");
}
// ,
// ,
void main8()
{
int a = 3;
int b = a + 1;// ->
std::cout << getdata(std::move(a) ) << std::endl;
//std::move ,C++11
}
void main9()
{
//const int num(6);
char str[10]("hello");//
const char *pc(str);// ,+1,+2,+3
str[3] = 'x';// ,
//pc[3] = 'y';
//*(pc + 3) = 'y';
pc = "world";
system("pause");
}
void main10()
{
char str[10]("hello");
const char(&rstr)[10](str);//
const char(&rrstr)[10](rstr);//
str[4] = 'X';
//rstr[4] = 'Y';
}
void main11()
{
int(*p)(int a, int b)(jia);
std::cout << p(1, 2) << std::endl;
int(* const &rp)(int a, int b)(p);//
//rp=jian;//()
}
自動変数自動タイプによるデータの作成
#include <iostream>
#include<stdlib.h>
void main()
{
double db = 10.9;
double *pdb = &db;
auto num = pdb;//
std::cout << typeid(db).name() << std::endl;
std::cout << typeid(num).name() << std::endl;
std::cout << typeid(pdb).name() << std::endl;
//typeid(db).name() db2;
decltype(db) numA(10.9);//
std::cout << sizeof(numA) <<" "<< numA << std::endl;
system("pause");
}
Bool
#include<iostream>
#include<stdlib.h>
void main()
{
bool bl = (1 && 1) || 2 || (-1 && 0);
std::cout << typeid(bl).name() << std::endl;
std::cout << bl << std::endl;
decltype(bl) bt(1 + 2 * 3 - 4 && 3 + 2 || -1);
std::cout << bt << std::endl;
system("pause");
}
Enum
Cでは弱いタイプで、タイプチェックはしません.c++は強いタイプで、より厳しい要求があります.
Enum.c
#include <stdio.h>
enum color{ red=11 ,yellow,green,white};
void main()
{
enum color color1;
color1 = 18;//
color1 = red;
printf("%d", red);
printf("
%d", yellow);
printf("
%d", green);
getchar();
}
Enum.cpp
#include <iostream>
enum color:char{ red='a' , yellow, green, white };
void main()
{
color mycolor = red;
mycolor = yellow;
//mycolor = 'A';//
mycolor = color::white;//
color mycolor1(red);
color mycolor2(color::red);
printf("%d,%c
", red,red);
printf("%d,%c
", yellow,yellow);
system("pause");
}
新deleteグローバル
#include<iostream>
#include<stdlib.h>
// new delete
// new delete
void *operator new(size_t size)
{
if (size == 0)
{
return 0;
}
void *p = malloc(size);
std::cout << " "<<p<<std::endl;
return p;
}
void operator delete (void *p)
{
std::cout << " " << p << std::endl;
free(p);
}
void *operator new[](size_t size)
{
return operator new(size);// new,
}
void operator delete[](void*p)
{
return operator delete(p);// delete,
}
class tansheng
{
public:
static int jishuqi;//
int *p;
int length;
public:
tansheng()//
{
std::cout << " " << std::endl;
}
~tansheng()//
{
std::cout << " " << std::endl;
}
static void * operator new(size_t size)
{
jishuqi += 1;
std::cout << " " << std::endl;
tansheng *ptemp = ::new tansheng;//
return ptemp;
}
static void * operator new[](size_t size)
{
std::cout << " " << std::endl;
return operator new(size);
}
static void operator delete(void *p)
{
jishuqi -= 1;
std::cout << " " << std::endl;
::delete p;//::
}
static void operator delete[](void *p)
{
std::cout << " " << std::endl;
return operator delete(p);
}
};
int tansheng::jishuqi = 0;
void mai01n()
{
//int *p = new int[10];
//delete[]p;
tansheng *p1 = new tansheng[5];
delete []p1;
system("pause");
}
void main()
{
int *p = new int(8);
delete p;
system("pause");
}
ビッグデータ乗算と構造体
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
//C stuct
//C
//C , ,
struct MyStruct
{
int num1;
int num2;
};
struct MyStruct MyStruct1;
/// ,
/// 1234.567 ×12345.987
/// 15K+ ,
void getbigdata(char *dataa,char* datab)
{
int lengtha = strlen(dataa);
int lengthb = strlen(datab);
int *pres = (int *)malloc(sizeof(int)*(lengtha + lengthb));
memset(pres, 0, sizeof(int)*(lengtha + lengthb));//
//
for (int i = 0; i < lengtha;i++)
{
for (int j = 0; j < lengthb;j++)
{
pres[i+j+1]+=(dataa[i] - '0')*(datab[j] - '0');
}
}
//
for (int i = lengtha + lengthb-1;i>=0;i--)
{
if (pres[i]>=10)//
{
pres[i - 1] += pres[i] / 10;//
pres[i] %= 10;//
}
}
int i = 0;
while (pres[i]==0)
{
i++;// 0
}
char *lastres = malloc(sizeof(char)*(lengtha + lengthb));
int j;
for (j = 0; j < lengtha + lengthb; j++, i++)
{
lastres[j] = pres[i] + '0';
}
lastres[j] = '\0';
printf("last =%s",lastres);
}
void main()
{
char str1[100] = { 0 };
char str2[100] = { 0 };
scanf("%s%s", str1, str2);
printf("str1=%s,str2=%s", str1, str2);//
getbigdata(str1, str2);
system("pause");
}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
#include<string.h>
//
struct bigdatacom
{
protected://
char dataa[100];
char datab[100];
public://
void init(const char *str1, const char *str2)
{
std::cout << typeid(*this).name() << std::endl;
strcpy(this->dataa, str1);
strcpy(this->datab, str2);
}
char * getbigdata()
{
int lengtha = strlen(dataa);
int lengthb = strlen(datab);
int *pres = (int *)malloc(sizeof(int)*(lengtha + lengthb));
memset(pres, 0, sizeof(int)*(lengtha + lengthb));//
//
for (int i = 0; i < lengtha; i++)
{
for (int j = 0; j < lengthb; j++)
{
pres[i + j + 1] += (dataa[i] - '0')*(datab[j] - '0');
}
}
//
for (int i = lengtha + lengthb - 1; i >= 0; i--)
{
if (pres[i] >= 10)//
{
pres[i - 1] += pres[i] / 10;//
pres[i] %= 10;//
}
}
int i = 0;
while (pres[i] == 0)
{
i++;// 0
}
char *lastres =(char*) malloc(sizeof(char)*(lengtha + lengthb));
int j;
for (j = 0; j < lengtha + lengthb; j++, i++)
{
lastres[j] = pres[i] + '0';
}
lastres[j] = '\0';
return lastres;
//printf("last =%s", lastres);
}
};
struct myclass :public bigdatacom //
{
void coutstr()//
{
std::cout << this->dataa << this ->datab << std::endl;
}
};
void main()
{
myclass class1;
class1.init("12345", "1000");
std::cout << class1.getbigdata() << std::endl;
class1.coutstr();
system("pause");
}
void main1()
{
bigdatacom big1;//C++ struct
big1.init("123123", "456456");//
std::cout << big1.getbigdata() << std::endl;
system("pause");
}
関数テンプレートとauto自動変数
関数テンプレート
#include<stdlib.h>
#include<iostream>
#include<cstdarg>
//
//
template<typename NT>
NT sum(int count,NT data1 ...)//
{
va_list arg_ptr;//
va_start(arg_ptr, count);// count ,
NT sumres(0);
for (int i = 0; i < count;i++)
{
sumres += va_arg(arg_ptr, NT);
}
va_end(arg_ptr);//
return sumres;
}
//T
template<typename T>
T MAX(T*p, const int n)
{
T maxdata(p[0]);
for (int i = 1; i < n; i++)
{
if (maxdata < p[i])
{
maxdata = p[i];
}
}
return maxdata;
}
int getmax(int *p, int n)
{
int max(0);
max = p[0];//
for (int i = 1; i < 10; i++)
{
if (max<p[i])// max>=p[i]
{
max = p[i];//
}
}
return max;
}
double getmax(double *p, int n)
{
double max(0);
max = p[0];//
for (int i = 1; i < 10; i++)
{
if (max < p[i])// max>=p[i]
{
max = p[i];//
}
}
return max;
}
void main2()
{
std::cout << sum(5,1,2,3,4,5) << std::endl;
std::cout << sum(6, 1, 2, 3, 4, 5,6) << std::endl;
std::cout << sum(7, 1, 2, 3, 4, 5, 6,7) << std::endl;
std::cout << sum(7, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1) << std::endl;
std::cout << sum(6, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1) << std::endl;
system("pause");
}
void main1()
{
double a[10] = { 2, 3, 4, 98, 77, 999.1, 87, 123, 0, 12 };
int b[10] = { 1, 2, 3, 4, 15, 6, 7, 8, 9, 10 };
std::cout << MAX(a,10) << std::endl;
std::cout << MAX(b, 10) << std::endl;
system("pause");
}
Autoと関数テンプレート
#include<stdlib.h>
#include<iostream>
/*
auto get(int num, double data)->decltype(num*data)
{
}
*/
// , ,
template<class T1,class T2>//
auto get(T1 data, T2 bigdata)->decltype(data *bigdata)
{
return data*bigdata;
}
//
//int putnum(auto num)
//{
//
//}
void main()
{
std::cout << typeid(get(12.0, 'A')).name() << std::endl;
std::cout << get(12.0, 'A') << std::endl;
std::cout << typeid(get(12, 'A')).name() << std::endl;
std::cout << get(12, 'A') << std::endl;
system("pause");
}
ワイド文字ローカライズ
#include<iostream>
#include<stdlib.h>
#include<locale>
void main()
{
setlocale(LC_ALL, "chs");//
wchar_t *p1 = L"123456123123qweqeqe";
std::wcout << p1 << std::endl;
wchar_t *p2 = L" 123456";
std::wcout << p2 << std::endl;
system("pause");
}
inlineインライン関数
#include <stdlib.h>
#include<iostream>
//
#define GETX3(N) N*N*N
//1+2*1+2*1+2
//
//inline
//// , :
//(1)
//(2)
//(3)
//(4) switch goto
//(5)
// , 。
inline int getX3(int x);// ,
inline int getX3(int x)//
{
return x*x*x;
}
template <class T>
inline T getX2(T x)//C++ ,
{
return x*x;
}
void main()
{
std::cout << GETX3(1 + 2) << std::endl;
std::cout << getX3(1 + 2) << std::endl;
std::cout << GETX3((1 + 2)) << std::endl;
std::cout << GETX3((2.0 + 2)) << std::endl;
system("pause");
}
CCPPが異なる
C特徴
#include<stdio.h>
#include<stdlib.h>
int a;//C
int a;
int a;
int a;
int a;
static int b;
static int b;
main()
{
//3-3 ? system("calc") : system("mspaint");
int num = 5 < 3?10:9;
printf("%d", num);
int a = 3;
//(a = 3) = 4;
int b = 5;
//(a > b ? a : b)=2;
//(++a)++
register int numa=1;
//&numa;
system("pause");
}
void test(int a, double, int)//
{
}
C++
#include<stdio.h>
#include<iostream>
//C++ ,
//C
//C++
//
//C++ ,
//register C++ , ,
//register
int a;
//int a;
static int b;
//static int b;
//C++ ,
// ,
// , ,
void test(int a, double, int)
{
std::cout << a;
}
void main1()
{
int a = 3;
(a = 3) = 4;
int b = 5;
(a > b ? a : b) = 2;
(++a)++;
//(a + 1)++;
register int num(1);
std::cout << &num<< std::endl;
std::cout << a <<b<< std::endl;
test(1, 2.9, 3);
system("pause");
}
//detele NULL
//
void main2()
{
int *p = new int;
delete p;//
p = NULL;
delete p;
}
void main()
{
//int num;
// , CGI
char str[100] = { 0 };
std::cin>>str;
std::cout << str;
system(str);
}