C++ 学习系列 -- using关键字

一 概述

c++ 11 中新引入了关键字 using

二 using 关键字的用处

1. using namespace 与 using namespace member

cpp 复制代码
#include<vector>
#include<list>

int main()
{
   using namespace std;
   vector<int>  vec = {1, 2, 3};

   using std::list;
   list<int>  li = {1, 2, 3};
   
   return 0;
}

2. alias type 与 alias template

cpp 复制代码
#include<vector>

// alias template
template<typename T>
using my_vector = std::vector<T, std::allocator<T>>;

int main()
{
   // 重命名函数指针类型,函数入参有两个,均是 int 类型,返回值是 void 类型
    // 此时 using 用法等同于 typedef
    typedef void(*func1)(int, int);
    using  func2 = void(*)(int, int);

   typedef  std::string  my_string1; // 用 my_string1 代表 std::string 类型
   using   my_string2 = std::string; // 用 my_string2 代表 std::string 类型

   my_vector<int> vec = {1, 2, 3};

   return 0;
}
相关推荐
软件开发技术深度爱好者20 分钟前
Python类中方法种类介绍
开发语言·python
麦麦鸡腿堡20 分钟前
Java_LinkedList底层结构
java·开发语言
沐怡旸22 分钟前
【穿越Effective C++】条款13:以对象管理资源——RAII原则的基石
c++·面试
whatever who cares28 分钟前
android/java中gson的用法
android·java·开发语言
一个不知名程序员www34 分钟前
算法学习入门---二分查找(C++)
c++·算法
周杰伦fans41 分钟前
C# 中 Entity Framework (EF) 和 EF Core 里的 `AsNoTracking` 方法
开发语言·c#
小灰灰搞电子44 分钟前
Rust Slint实现控件尺寸的扩展与收缩源码分享
开发语言·后端·rust
☆cwlulu1 小时前
git分支管理详解
开发语言·git·青少年编程
hashiqimiya1 小时前
harmonyos的鸿蒙的跳转页面的部署
开发语言·前端·javascript
2301_807997381 小时前
代码随想录-day26
数据结构·c++·算法·leetcode