c++入门

#include<iostream>
using namespace std;//不加using namespace std;
int main() 
{
    cout << "helloworld" << endl;
    //std::cout << "helloworld" << std::endl;

    return 0;
}

#include<stdio.h>
int a = 2;
void f1() 
{
    int a = 0;
    printf("%d\n", a);//0
    printf("%d\n", ::a);//2//::域作用限定符(直接找全局域)
}


int main()
{
    printf("%d\n", a);
    f1();
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
//namespace 为命名空间域
//::域作用操作符
namespace bit
{
    int rand = 10;
    int Add(int x, int y)
    {
        return x + y;
    }
    struct Node
    {
        struct Node* next;
        int val;
    };
}
//域名不同相同函数不会产生冲突
//域名重复,可以通过域名嵌套的方法解决

namespace bit1
{
    namespace bit 
    {
        int rand = 10;
        int Add(int x, int y)
        {
            return x + y;
        }
        struct Node
        {
            struct Node* next;
            int val;
        };
    }
    
}
//1.先在当前局域找
//2.再到全局域找
//3.默认情况下不会到命名空间找
int main()
{
    printf("%p\n", rand);
    printf("%d\n", bit::rand);//指定命名空间
    printf("%d\n", bit::Add(10, 20));

    //struct bit::Node pnode;

    printf("%d\n", bit1::bit::Add(10, 90));


    return 0;
}

#include<iostream>
using namespace std;
int a = 0;
int main()
{
    int a = 1;
    cout << ::a << endl;//先找全局
    cout << a << endl;//先找局部
    return 0;
}

#include"list.h"
#include<stdio.h>
不同文件可以定义同名的命名空间(同名的命名空间会进行自动合并)

若list.h的中也含有stack则将list.h中的已及相关功能的代码 用域命名空间包含起来
struct stack
{

};

#include<iostream>
#include<stack>
using namespace std;//展开命名空间

namespace bit//不会影响生命周期
{
    stack<int> st;
}
//using namespace bit;//加声明默认会去找
//不展开就不怕冲突

//按理说应该先找全局的stack
//stack<int> st;


//指定展开某一个
using bit::st;
int main()
{
    /*bit::st.push(10);
    cout << bit::st.top();*/

    st.push(10);
    cout << st.top() << endl;
    //在主函数创建的指定命名空间的量,后面可以不用加域操作符

    return 0;
}

#include<iostream>//io流
//using namespace std;
//展开官方库的命名空间

//using std::cout;
//using std::cin;
//用哪个展开哪个
int main()
{
    int i = 0;
    double j = 1.11;
    //自动识别类型
    //cout << i << " " << j << endl;//<<流插入
    std::cout << i << " " << j << "\n" << std::endl;
    std::cin >> i;//流输入
    std::cout << i;
    return 0;
}

namespace 命名空间域  关键字//解决名字冲突
#include<iostream>
using namespace std;//会去std中去搜索
#include"list.h"
#include"Queue.h"
int main() 
{
    struct AQueue::Node node1;//命名空间可以循环嵌套//每次查找多加一层::
    struct Blist::Node node2;
    AQueue::min++;
    cout << AQueue::min;
}

1.全部展开(一般用于练习)
2.指定空间访问
3.部分展开
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
    cout << "hello" << endl;
}


#include<iostream>
using namespace std;
int main() 
{
    //<<流插入   >>流提取
    //endl=="\n"
    /*cout << "helloworld" << endl;
    cout << "helloworld" << '\n';*/

    //自动识别
    int n;
    cin >> n;
    double *a=(double*)malloc(sizeof(double)*n);
    if (a == NULL)
    {
        perror("malloc fail");
        return 0;
    }
    
    for (int i = 0; i < n; i++) 
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) 
    {
        cout<< a[i]<<endl;
    }
    return 0;
}

缺省参数
#include<iostream>
using namespace std;
void func(int a = 0)
{
    cout << a << endl;
}
int main()
{
    func(1);//传参就没有0什么事了
    func();//不传则为0
    return 0;
}
 
 
 
 

#include<iostream>
using namespace std;
void func(int a = 10, int b = 20, int c = 30)
{
    cout << "a=" << a << endl;
    cout << "b=" << b << endl;
    cout << "c=" << c << endl;
}
int main()
{
    func(1, 2, 3);
    func(1, 2);
    func(1);//不能跳跃缺省  必须从右往左使用
    func();
    return 0;
}
 
 
 

半缺省参数//如果不知道传多少就用半缺省
#include<iostream>
using namespace std;
void func(int a , int b = 20, int c = 30)
{
    cout << "a=" << a << endl;
    cout << "b=" << b << endl;
    cout << "c=" << c << endl;
}
int main()
{
    func(1, 2, 3);
    func(1, 2);
    func(1);//不能跳跃缺省  必须从右往左使用
    
    return 0;
}


缺省参数一般在声明的时候给(不能声明和定义同时存在)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值