zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++ 命名空间 实战(一)

C++ 实战 空间 命名
2023-09-14 09:09:30 时间
#include <iostream>
using namespace std;

// 第一个命名空间
namespace first_space {
    void func() {
        cout << "Inside first_space" << endl;
    }
}
// 第二个命名空间
namespace second_space {
    void func() {
        cout << "Inside second_space" << endl;
    }
}
int main()
{

    // 调用第一个命名空间中的函数
    first_space::func();

    // 调用第二个命名空间中的函数
    second_space::func();

    return 0;
}
Inside first_space
Inside second_space
#include <iostream>
using namespace std;

// 第一个命名空间
namespace first_space {
    void func() {
        cout << "Inside first_space" << endl;
    }
}
// 第二个命名空间
namespace second_space {
    void func() {
        cout << "Inside second_space" << endl;
    }
}
using namespace first_space;
int main()
{

    // 调用第一个命名空间中的函数
    func();

    return 0;
}
Inside first_space
#include <iostream>
using std::cout;
 
int main ()
{
 
   cout << "std::endl is used with std!" << std::endl;
   
   return 0;
}
std::endl is used with std!