c++ - 可以写“使用 std::vector;”吗?在标题中?而不是写 std::vectorv 每次?
问题描述
因为,我知道写“使用命名空间标准;” 应避免在标题中。现在我发现每次在代码中都在向量、字符串、cout、cin 等之前写“std::”很烦人。所以,我可以在标题中写“使用 std::vector”、“使用 std::cout”等,这样我就不必一次又一次地写它?而且,我可以写“使用命名空间标准;” 在编码面试的标题中?
我可以使用如下所示的方式吗?
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using std::endl;
using std::vector;
using std::string;
using std::cout;
using std::pair;
解决方案
你可以typedef
这样使用
typedef std::vector<int> int_vec;
你可以稍后在这样的代码中使用它
int_vec v;
或者如果你不关心可读性,你应该这样做
typedef std::vector<int> v;
你可能也想看看这个https://docs.microsoft.com/en-us/cpp/cpp/aliases-and-typedefs-cpp
或者
你也可以using
像忍者说的那样使用
using int_vec = std::vector<int>;
请参阅我在上面发布的文档链接
推荐阅读
- c# - 如何在 ASP.NET C# 具有 @ 的字符串 1 处输入字符串 2 值?
- c++ - Lua库没有链接
- html - 最小化屏幕时在 CSS 中的文本定位
- python - 根据特定列值组合 Dataframe 上的行并添加其他值
- flutter - 将字符串转换为日期?
- c++ - 过多的光标引用突出显示 VS 社区
- python-3.x - 连接到外部 SQLite 数据库
- java - java - ScheduledThreadPoolExecutor 线程中的可见性
- python - 将当前图像转换为黑白
- jasper-reports - 如何在 Jasper Report 中对列的数据求和