C++小知识点

记录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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值