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