c++最简单结构

首先只能在一个CPP文件中声明全局变量,另外最好加个包含声明变量的头文件,这样就基本上解决问题了。

比如: //--------------------------------------- // 全局变量ValueDef.h中声明如下:

 #ifndef INCLUDE_G_VALUE

#define INCLUDE_G_VALUE

extern int g_iTest; // 声明引用全局变量一

extern int g_dTest; // 声明引用全局变量二

#endif //

--------------------------------------- // 定义全局变量

Test.CPP中声明

int g_iTest; // 声明全局变量一

int g_dTest; // 声明全局变量二 // ......

//--------------------------------------- //

在使用的变量的地方, 添加 #include "ValueDef.h" // 就行了...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ STL 中的 `tree` 是一个非标准的容器,需要使用第三方库 `GCC-Tree` 才能使用。下面是一个简单的例子,演示如何使用 `tree` 存储一组整数,并对其进行遍历和查找操作: ```c++ #include <iostream> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int main() { ordered_set s; s.insert(10); s.insert(20); s.insert(30); s.insert(40); s.insert(50); cout << "size = " << s.size() << endl; cout << "s[1] = " << *s.find_by_order(1) << endl; cout << "s[2] = " << *s.find_by_order(2) << endl; cout << "s.rank(30) = " << s.order_of_key(30) << endl; cout << "s.rank(40) = " << s.order_of_key(40) << endl; for (auto x : s) { cout << x << " "; } return 0; } ``` 输出结果为: ``` size = 5 s[1] = 20 s[2] = 30 s.rank(30) = 2 s.rank(40) = 3 10 20 30 40 50 ``` 在这个例子中,我们首先定义了一个 `ordered_set` 类型的变量 `s`,它是一个存储整数类型元素的 `tree` 容器。然后,我们向 `s` 中插入了一些元素,使用 `size` 函数查看容器中元素的个数,使用 `find_by_order` 函数查找元素,使用 `order_of_key` 函数查找元素在容器中的排名,最后使用一个 for 循环遍历容器中的元素并输出。 需要注意的是,`tree` 是一个非标准的容器,使用时需要在编译选项中添加 `-std=c++11 -D_GLIBCXX_DEBUG`。此外,`tree` 容器的功能与 `set` 和 `map` 类似,但是由于 `tree` 使用的是红黑树,因此插入和删除操作的时间复杂度要比 `set` 和 `map` 高一些,但是查找操作的时间复杂度相对较低。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值