I came across an expression that states: vector<int> pair(n)
. As pair is an inbuilt structure in itself, can we use it as a variable name in c++ and why??

- 32,622
- 4
- 31
- 60

- 43
- 4
-
4`using namespace std` can be confusing, yes. Please show a [mcve] – 463035818_is_not_an_ai Apr 26 '21 at 08:21
-
2Here's the list of keywords you can't use: https://en.cppreference.com/w/cpp/keyword – m88 Apr 26 '21 at 08:21
-
1there's no problem using names from the standard library as long as you aren't using `using namespace std` – Alan Birtles Apr 26 '21 at 08:23
-
2You can even have a vector called vector: `std::vector
vector{ 1,2,3 };` is perfectly valid code (if a little odd). – Adrian Mole Apr 26 '21 at 08:24 -
this very much depends on what headers you include and what using directive is in the code. – 463035818_is_not_an_ai Apr 26 '21 at 08:24
-
1Very related, if not an actual duplicate: [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/10871073) – Adrian Mole Apr 26 '21 at 08:28
-
The question is a bit incomplete, so we can't assume `using namespace std;`. It might have said `using std::vector;`, in which case `std::pair` is not brought in. – MSalters Apr 26 '21 at 08:30
-
1I don't understand the obsession with `using namespace std;` for this question. Even with that, you could do something like `vector
vector;`, it doesn't suddenly prevent you from shadowing a type name with a variable name just because the type name is in scope. – chris Apr 26 '21 at 08:32 -
@chris it does if you want to use `std::vector` again in the same scope: https://godbolt.org/z/GWrf75nWd – Alan Birtles Apr 26 '21 at 08:52
-
@AlanBirtles, Yes, that's a property of shadowing, not of the using directive. – chris Apr 26 '21 at 08:52
-
@chris that code would have compiled fine if you used `std::vector` instead of using namespace std + unqualified vector – Aykhan Hagverdili Apr 26 '21 at 09:12
3 Answers
Yes, you can. The templates from the standard library are called std::pair
and std::vector
. They are not built-in, in the sense that they aren't part of the language, but part of the standard library. Hence this is not a problem:
#include <vector>
std::vector<int> pair(n);
One can use using directives to drop the std::
prefix:
#include <vector>
using std::vector;
vector<int> pair(n);
Commonly used is also using namespace std;
:
#include <vector>
using namespace std;
vector<int> pair(n);
Though, once you pulled the complete namespace into scope, the code can be rather confusing.
Read Why is “using namespace std;” considered bad practice? and try to avoid it. And give proper names. std::pair
is rather common, so you better choose a better name for the vector, it certainly is not a pair
.

- 109,796
- 11
- 89
- 185
As pair is an inbuilt structure in itself, can we use it as a variable name in c++ and why?
That is not correct. The Standard Template Library provides a class template called "pair" but that is not an inbuilt type and, furthermore, "pair" is neither a keyword nor a reserved word in the C++ language.
Furthermore, the aforementioned "pair" template is actually provided (in the <utility>
header) in the std
namespace, so its fully qualified name is std::pair
. So, unless you both #include <utility>
(explicitly or implicitly) and are using std::pair;
(or using namespace std;
– but see here), you can use the name, "pair," as an identifier, just like any other non-reserved word.
In fact, even if you do #include <utility>
and are using std::pair;
(or using namespace std;
), you can still use "pair" as an identifier, as mentioned the comment by chris.

- 49,934
- 160
- 51
- 83
-
I'd say "_The C++ standard library_" instead of "_The Standard Template Library_" though. – Ted Lyngmo Apr 26 '21 at 08:43
-
If you haven't included
, that doesn't necessarily mean pair isn't defined in std namespace. It's possible that some other standard header you include includes – Aykhan Hagverdili Apr 26 '21 at 09:08or defines pair. unordered_map, for instance, certainly does. -
@AyxanHaqverdili Notice that I mentioned inclusion of that header, "explicitly *or implicitly*." From what I can tell, if `std::pair` is defined, it will be defined in that header (but I'd need to thoroughly check the Standard, to be sure). – Adrian Mole Apr 26 '21 at 09:11
-
@AdrianMole it's very possible that there is a third header that only defines`pair` (not other things
defines) and both the headers ( – Aykhan Hagverdili Apr 26 '21 at 09:13and some other header you used) include that. -
@AyxanHaqverdili [This Draft C++17 Standard](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf) strongly suggests (Section 23.1, Table 34) that "pairs" are defined in the `
` header. – Adrian Mole Apr 26 '21 at 09:19
std::pair
is not an "in-built structure". It's a qualified name from the Standard Library. Since it's qualified, it generally does not clash with your own names.
This is exactly why namespaces were added to the C++ language.

- 173,980
- 10
- 155
- 350