C过渡到C++/基础

#include <iostream> //input output stream 输入输出流
using namespace std;//使用std 的名称空间,cin cout在其中,也可在cin\cout    前加std::
#include<string>

int main()
{
    cout << "hello world" << endl; //end of line换行
    int a = 10;
    cout << "a=" << a << endl;
    cout << a << endl;

    cout << "a所占内存空间为:" << sizeof(a) << endl;
    cout << "int所占内存空间为:" << sizeof(int) << endl;
    cout << "int*所占内存空间为:" << sizeof(int*) << endl;

    //转义字符  \n--换行符  \\--反斜杠  \t--水平制表符
    cout << "\\" << endl;
    cout << "abcd\thahaha" << endl; //具有对齐效果
    cout << "ab\thahaha" << endl;

    //字符串风格 string类
    char arr[] = "abcde";
    string brr = "abcde";
    cout << arr << endl;
    cout << brr << endl;
    cout << arr + brr << endl;//可以相加

    //bool数据类型(1字节);true--真(非0) false--假(0)
    bool flag1 = true;
    bool flag2 = false;
    cout << flag1 << endl;
    cout << flag2 << endl;
    cout << sizeof(bool) << endl;

    //地址
    int crr[5] = { 1,2,3,4,5 };
    cout << crr << endl;
    cout << &crr[0] << endl;
    cout << (int)crr << endl;

    //new 在堆区开辟数据
    int *p = new int(10);
    cout << *p << endl;
    delete p;
    p = NULL;

    int* pa = new int[10];
    for (int i = 0; i < 10; i++)
        pa[i] = i + 100;
    for (int i = 0; i < 10; i++)
        cout << pa[i] << endl;
    delete[] pa;

    //引用:给变量起别名  数据类型 &别名=原名
    int i = 10;
    int& j = i;  //引用必须初始化,且初始化后不可更改
    cout << j << endl;
    j = 11;
    cout << i << endl;
  //int &a=10;--error
  //const int &a;--ok--会自动生成临时变量int tmp=10;const int &a=tmp;

    //C++的数据输入
    string w = "adsdf";
    bool h = false;

    cout << "\n\n给布尔类型h赋值" << endl;
    cin >> h;
    cout << h << endl;    //只要是非0值都代表真

    system("pause");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值