记录c++学习中的小知识点.
1.输入输出
#include <iostream>
using namespace std;
/************************/
/*
这是第一个c++程序
*/
/************************/
int main(){
cout<<"hello world"<< " yunlan";
cout<<endl;
int x=0;
cout <<"请输入一个整数:" << endl;
cin >> x;
cout << oct << x <<endl;
cout << dec << x <<endl;
cout << hex << x <<endl;
bool y = false;
cin >> y;
cout << boolalpha << y << endl;
system("pause");
return 0;
}
2.命名空间
#include <iostream>
#include <stdlib.h>
using namespace std;
/************************/
/*
命名空间
*/
/************************/
namespace mynum
{
int x = 105;
}
int main(void)
{
bool isflag = false;
if(mynum::x % 2 == 0)
{
isflag = true;
}
else
{
isflag = false;
}
if(isflag)
{
cout << "偶数" << endl;
}
else
{
cout << "基数" << endl;
}
system("pause");
return 0;
}
3.引用
#include <iostream>
#include <stdlib.h>
using namespace std;
/************************/
/*
引用
*/
/************************/
int main(void)
{
/*基本数据类型的引用*/
int a = 3;
int &b = a;
b = 10;
cout << "a="<<a << endl;
/*指针的引用*/
int c = 10;
int *p = &c;
int *&q = p;
*q = 20;
cout << "c="<< c <<endl;
/*结构体的引用*/
typedef struct MyStruct
{
int x;
int y;
}Coor;
Coor c1;
Coor &coor = c1;
coor.x = 10;
coor.y = 20;
cout << c1.x <<","<<c1.y <<endl;
system("pause");
return 0;
}
4.堆内存、ram
#include <iostream>
#include <stdlib.h>
using namespace std;
/************************/
/*
堆内存,ram
*/
/************************/
int main(void)
{
int a = 10;
int *p = &a;
cout<<*p<<","<<p<<endl;
char *str = new char[10];
strcpy(str,"hello");
cout<<str<<endl;
for(int i=0;i<10;i++)
{
cout<<str[i];
}
delete []str;
str = NULL;
system("pause");
return 0;
}