首页 > 解决方案 > 可以写“使用 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;

标签: c++

解决方案


你可以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>;

请参阅我在上面发布的文档链接


推荐阅读