Using std Namespace

前端 未结 16 728
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 04:57

There seem to be different views on using \'using\' with respect to the std namespace.

Some say use \' using namespace std\', other say don\'t but rathe

16条回答
  •  悲&欢浪女
    2020-11-22 05:19

    For me, I prefer to use :: when possible.

    std::list iList;
    

    I hate to write :

    for(std::list::iterator i = iList.begin(); i != iList.end(); i++)
    {
        //
    }
    

    Hopefully, with C++0x I would write this:

    for(auto i = iList.begin(); i != iList.end(); i++)
    {
        //
    }
    

    If the namespace is very lengthy,

    namespace dir = boost::filesystem;
    
    dir::directory_iterator file("e:/boost");
    dir::directory_iterator end;
    
    for( ; file != end; file++)
    {
        if(dir::is_directory(*file))
            std::cout << *file << std::endl;
    }
    

提交回复
热议问题