namespace
2015-11-07 00:00
330 查看
摘要: C++ namespace
以上给出了简单的namespace应用实例。
Namespaces (C++) A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
以下程序的功能是根据用户需要求出数组元素的最大或者最小值。当isMax=1时求最大值,反之,求最小值。
结果分别输出了最大值和最小值。
#include <stdlib.h> #include <iostream> namespace A { int x = 1; void fun() { std::cout << "A" << std::endl; } } namespace B { int x = 2; void fun() { std::cout << "B" << std:: endl; } void fun2() { std::cout << "2B" << std::endl; } } using namespace B; int main(void) { std::cout << A::x << std::endl; B::fun(); fun2(); system("pause"); return 0; }
以上给出了简单的namespace应用实例。
Namespaces (C++) A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
以下程序的功能是根据用户需要求出数组元素的最大或者最小值。当isMax=1时求最大值,反之,求最小值。
#include <stdlib.h> #include <iostream> using namespace std; namespace CompA { int getMaxOrMin(int *arr, int count, bool isMax) { int temp = arr[0]; for (int i = 1; i < count; i++) { if (isMax) { if (temp < arr[i]) { temp = arr[i]; } } else { if (temp > arr[i]) { temp = arr[i]; } } } return temp; } } int main(void) { int arr1[4] = { 3,5,1,7 }; bool isMax=false; cin >> isMax; cout<<CompA::getMaxOrMin(arr1, 4, isMax)<<endl;//namespace CompA system("pause"); return 0; }
结果分别输出了最大值和最小值。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Flex Namespace的用法
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性