I figured out that one can use a lot of ( perhaps all the) functions of the algorithm
library with or without namespace std
: e.g. when algorithm
is imported:
#include <algorithm>
std::unique
and unique
seem to be equivalent. Here is an example:
#include <iostream>
#include <vector>
#include <algorithm>
int main () {
std::vector<int> v = {10,20,20,20,30,30,20,20,10};
std::vector<int>::iterator it;
it = std::unique (v.begin(), v.end());
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
it = unique (v.begin(), v.end());
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
The output:
10 20 30 20 10 30 20 20 10
10 20 30 20 10 30 20 20 10
1) Are they the same functions?
2) What is the mechanism that enables using these functions regardless the use of the std
namespace? I looked up the source code:
https://github.com/gcc-mirror/gcc/blob/d9375e490072d1aae73a93949aa158fcd2a27018/libstdc%2B%2B-v3/include/bits/stl_algo.h
and
but I have still no idea about how that works.
Thank you in advance.