jeudi 30 mars 2017

Why are functions in

This question already has an answer here:

I have the following basic code that creates a vector and tries finding an element in it. I noticed that you don't need to specify the std:: namespace to call find and was wondering if there was any particular reason the functions in <algorithm> are not wrapped in namespace std? I ask because I was recently writing a find method and encountered an ambitious compiler error.

Isn't the whole point of namespaces to avoid this sort of thing?

#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;    using std::endl;
using std::vector;  using std::string;


int main() {
    std::vector<int> v = {1,5,6,9,3,4};
    auto i = find(v.begin(), v.end(), 3);       // why does this work without std:: at the beginning
    cout << "Output: " << *i << endl;
}

Aucun commentaire:

Enregistrer un commentaire