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;
}
相关推荐
blasit13 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_2 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星2 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛4 天前
delete又未完全delete
c++
端平入洛5 天前
auto有时不auto
c++
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1235 天前
matlab画图工具
开发语言·matlab
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言