Search code examples
c++variablesstd-pair

Can we use pair as the name for a vector?


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??


Solution

  • 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.