1.引用头文件
c | c++ |
---|---|
#include <stdio.h> | #include < iostream > / #include < cstdio > |
#include <stdlib.h> | #include < cstdlib > |
#include <string.h> | #include < cstring > |
#include <math.h> | #include < cmath > |
2.函数重载
- C++支持函数重载
//#include <iostream>
#include <cstdio>
using namespace std;
void printf(){
printf("Hello World!\n");
}
int main(){
printf();
}
3.命名空间
- C++支持命名空间:避免全局变量、函数、类的命名冲突(因名字相同而编译失败)
namespace 空间名{
定义类//函数
}
using namespace 空间名;
using std::cout; //using 空间名::标识符;
#include <iostream>
#include <cstdio>
using namespace std;
namespace salt{
void printf(){
cout << "Hello Salt" << endl;
}
}
namespace sugar{
void printf(){
cout << "Hello Sugar" << endl;
}
}
using namespace sugar;
int main(){
//salt::printf();
//sugar::printf();
printf();
}
4.类型
- bool:true – false 内置类型和常量。
5.动态内存
*p = new void; //申请void型的动态内存
delete p; //释放内存
int* p = new int[n];
delete [] p;
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int main(){
//C
int* p = (int*)malloc(sizeof(int));
*p = 100;
printf("%p %d\n",p,*p);
//C++
int* q = new int;
*q = 200;
cout << q << " " << *q << endl;
//C
free(p);
p = NULL;
//C++
delete q;
q = NULL;
//C
int n;
scanf("%d",&n);
int* arr = (int*)malloc(sizeof(int)*n);
for (int i=0;i<n;i++){
scanf("%d",arr+i);
}
for (int i=0;i<n;i++){
printf("%d\n",arr[i]);
}
free(arr);
arr = NULL;
//C++
{
int n;
cin >> n;
int* parr = new int[n];
for (int i=0;i<n;i++){
cin >> parr[i];
}
for (int i=0;i<n;i++){
cout << parr[i] << endl;
}
delete [] parr;
parr = NULL;
}
//C
{
int n;
scanf("%d",&n);
getchar();
char* arr = (char*)malloc(sizeof(int)*(n+1));
for (int i=0;i<n;i++){
scanf("%c",arr+i);
}
arr[n] = '\0';
for (int i=0;i<n;i++){
printf("%c\n",arr[i]);
}
free(arr);
arr = NULL;
}
//C++
{
int n;
cin >> n;
char* parr = new char[n];
/*
for (int i=0;i<n;i++){
cin >> parr[i];
}
*/
cin >> parr;
parr[n] = '\0';
cout << parr << endl;
/*
for (int i=0;i<n;i++){
cout << parr[i] << endl;
}
*/
delete [] parr;
parr = NULL;
}
}
- realloc
int* temp = new int[size];
for (int i=0;i<n;i++){
temp[i] = data[i];
}
// 可用memcpy()
// memcpy(temp,data,sizeof(Elemeent)*size)
delete [] data;
data = temp;
- 栈 -顺序表
#include <iostream>
// 栈 - 顺序表 // 倒序输出
using namespace std;
typedef int Element;
class Stack{
Element* data;
size_t size;
public:
// 初始化
void Create(){
data = NULL;
size = 0;
}
void Destory(){
delete [] data;
data = NULL;
size = 0;
}
size_t GetSize(){
return size;
}
// 入栈
bool Push(int e){
// realloc();
Element* temp = new Element[size+1];
if(NULL == temp) return false; // 没有申请到内存
// 将data[]中的数复制到temp中
for (int i=0;i<size;i++){
temp[i] = data[i];
}
delete [] data; // 释放data;
data = temp; // 重新将temp赋值给data
data[size] = e; // 数组长度size+1 data[size] 为数组最后一个元素
size++;
return true;
}
// 出栈
bool Pop(){
if(NULL == data) return false; // 空栈不出栈
--size; // 栈长度减一
Element* temp = new Element[size];
if(NULL == temp) return false;
for (int i=0;i<size;i++){
temp[i] = data[i];
}
delete [] data;
data = temp;
return true;
}
// 获得栈顶元素
Element* GetTop(){
return &data[size-1];
}
};
int main(){
Stack s;
s.Create(); // 初始化栈
for (;;){
int e;
if(!(cin >> e)) break;
s.Push(e);
}
while (0 != s.GetSize()){
cout << *s.GetTop() << endl;
s.Pop();
}
s.Destory();
}
6.初始化
int n(num);
#include <iostream>
using namespace std;
int main(){
//C
int n = 10;
float f = 1.1;
printf("%d\n%f\n",n,f);
int arr[3] = {1,2,3};
for (int i=0;i<3;i++){
printf("%d\n",arr[i]);
}
int* p = (int*)malloc(sizeof(int)); // 防止野指针
*p = 10;
printf("%d\n",*p);
char c = 'a';
printf("%c\n",c);
char* s = (char*)"abc";
printf("%s\n",s);
//C++
{
int n(20);
float f(2.2);
cout << n << endl << f << endl;
int arr[3] = {3,4,5};
for (int i=0;i<3;i++){
cout << arr[i] << " ";
}
cout << endl;
int* p = new int; // 防止野指针
*p = 200;;
cout << *p << endl;
char c('z');
cout << c << endl;
char* s((char*)"def");
cout << s << endl;
}
}
版权声明:本文为qq_43113093原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。