g++编译宏使用

C++类中的权限

在C++中
private成员函数只能在类内使用,是不对外开放的
public成员函数,既能在类内使用,又能在类外使用
private成员只能在类内使用

编译宏使用

#include<iostream>
#include <vector>
#include <algorithm>
#include <istream>
#include <string>
#include <sstream>
#include <set>

#ifdef BNode
struct BNode
{
    /* data */
    BNode* left_son;
    BNode* right_son;
    int data;

    BNode(int a):data(a),left_son(nullptr),right_son(nullptr)
    {
        
    }
};

int find_depth(BNode* bn)
{
    
    if(bn==nullptr/* && bn->right_son==nullptr*/)
    {
        return 0;
    }

    // depth++;
    int left_depth = find_depth(bn->left_son);

    int right_depth = find_depth(bn->right_son);
        /* code */

    // depth++;
    return right_depth>left_depth?right_depth+1:left_depth+1;

}

#endif

#ifdef vivo
// 输出不能被7整除的工号 vector
bool find_num( const std::vector<int>& vec, int& count)
{
    bool found= false;
    std::vector<int> has7;
    std::set<int> has7_set;
    std::set<int> all_set;
    for(const auto& ele:vec)
    {
        all_set.insert(ele);
        if(ele%7==0 && ele!=0)
        {   
            has7_set.insert(ele);
            count++;
            found=true;
            has7.push_back(ele);
        }
    }

    // 计算差
    std::set<int> out;
    std::set_difference(all_set.begin(), all_set.end(), has7_set.begin(), has7_set.end(), std::inserter(out, out.begin()));
    int count2 = 0;
    for(const auto& ele:out)
    {
        std::string tmp_string(std::to_string(ele));
        // if(std::find(tmp_string.begin(), tmp_string.end(), "7")!=);
        auto res = tmp_string.find("7");
        if(res!=tmp_string.npos)
        {
            count2++;
            found = true;
        }


    }
    count +=count2;
    

    return found;  
}
#endif

#ifdef BASE
class Base
{
public:
    Base(int data):_data(data){
        std::cout<< " constructor " << std::endl;
    }
    int data(){return _data;}
    void set_data(int data){_data = data;}

    void func(){
        private_fun();
    }
    
private:
    void private_fun(){
        auto a = data();
        a++;
        set_data(a);
        
    }


private:
    int _data;
};
#endif

int main(int argc, char const *argv[])
{
#ifdef vivo
    std::string line;
    std::string word;
    std::vector<int> vec_num;
    std::getline(std::cin, line);
    std::stringstream in_stream(line);
    while(in_stream>>word)
    {
        vec_num.push_back(std::atoi(word.c_str()));
    }
    int count =0 ;
    if(find_num(vec_num, count))
    {
        std::cout<<count<<std::endl;
    }
    else{
        std::cout<<0<<std::endl;
    }
    return 0;
#endif
#ifdef BASE
    Base* b = new Base(29);
    std::cout<< "data: "<< b->data() <<std::endl;
    b->func();
    std::cout<<"data: "<< b->data()<<std::endl; 
#endif
}

在终端输入如g++ test1.cpp -o test1 -DBASE的命令,就能实现对程序中每一块的编译控制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值