简体   繁体   English

是否应该避免或鼓励“使用 std::cin”和“使用 std::cout”?

[英]Should `using std::cin` and `using std::cout` be avoided or encouraged?

I searched this site and people says you should avoid using using namespace std .我搜索了这个站点,人们说你应该避免使用using namespace std I totally agree.我完全同意。 However, what about using std::cin and using std::string ?但是, using std::cinusing std::string怎么样? Should this be avoided or encouraged?应该避免或鼓励这种情况吗?

I know always type std::cin is the safest choice, but it is very tedious to type them again and again.我知道总是输入std::cin是最安全的选择,但是一遍又一遍地输入它们是非常乏味的。

However, when you type using std::cin etc in the begining of the file, it seems very crowd.但是,当您在文件的开头using std::cin等键入时,它似乎非常拥挤。 For example, this simple program read and calculate student grade, in front of it, there are too many using std:: , it look very uncomfortable.比如这个简单的读取和计算学生成绩的程序,在它前面, using std::太多了,看起来很不舒服。

#include <iostream>
#include <ios>
#include <iomanip>
#include <stdexcept>
#include <vector>
using std::cin;             using std::cout;
using std::istream;         using std::vector;
using std::setprecision;    using std::domain_error;
using std::string;          using std::getline;
using std::streamsize;

istream& read_hw(istream& in, vector<double>& homework);
double grade(double mid_exam, double final_exam, \
        const vector<double>& homework);

int main()  {

    std::string name;
    std::getline(std::cin, name);
    std::cout << "Hello, " + name + "!" << std::endl;

    double mid_exam, final_exam;
    std::cin >> mid_exam >> final_exam;

    std::vector<double> homework;
    read_hw(std::cin, homework);

    try {
        double final_grade = grade(mid_exam, final_exam, homework);
        std::streamsize prec = std::cout.precision();
        std::cout << "your final grade is:" << std::setprecision(3)
            << final_grade << std::setprecision(prec) << std::endl;
    }
    catch(std::domain_error)    {
        std::cout << std::endl << "No homework entered!" << std::endl;
        return 1;
    }

    return 0;
}

std::istream& read_hw(std::istream& in, std::vector<double>& homework)   {
    if(in)  {
        homework.clear();
        double x;
        while(in >> x)  {
            homework.push_back(x);
        }
    }
    in.clear();

    return in;
}

double grade(double mid_exam, double final_exam, \
        const std::vector<double>& homework)    {
    std::vector<double>::size_type i, size;
    size = homework.size();
    if(size ==0)    {
        throw std::domain_error("no homework grade entered!");
    }
    double sum = 0;
    double average = 0;
    for(i = 0; i < size; ++i)   {
        sum += homework[i];
    }
    average = sum/size;

    return mid_exam*0.3 + final_exam*0.3 + average*0.4;
}

In python's tutorial , it says:python的教程中,它说:

Remember, there is nothing wrong with using from Package import specific_submodule !请记住,使用from Package import specific_submodule没有任何问题! In fact, this is the recommended notation unless the importing module needs to use submodules with the same name from different packages.事实上,这是推荐的表示法,除非导入模块需要使用来自不同包的同名子模块。

I want to know what I should do in c++ programs.我想知道我应该在 C++ 程序中做什么。

Never use using namespace std;永远不要使用using namespace std; or similar in a header file as it can cause all sorts of ambiguity issues that arise due to namespace pollution .或在头文件中类似,因为它可能导致由于命名空间污染而出现的各种歧义问题。 If you obey that rule then folk who have to include your headers will thank you for it.如果您遵守该规则,那么必须包含您的标题的人会为此感谢您。 I'd also avoid any sort of using std::... in headers for similar reasons.出于类似的原因,我也会避免在标题中using std::...任何类型的using std::... Learn to love the more verbose std:: notation.学会喜欢更冗长的std::符号。

What you get up to in a source file is largely down to you.您在源文件中所做的工作很大程度上取决于您。 Any recommendations here are largely opinion-based, but personally, I never use using just to save typing, but rather when it's unavoidable such as bringing shadowed functions back into a namespace, and in template metaprogramming.这里的任何建议主要是基于意见的,但就个人而言,我从不使用using只是为了节省输入,而是在不可避免的情况下使用,例如将阴影函数带回命名空间,以及模板元编程。

Imho this question is rather opinion based.恕我直言,这个问题是基于意见的。 However, this is my opinion:不过,这是我的看法:

Dont use:不要使用:

using std::cin;
using std::cout;
using std::string; 

or the like.或类似。 My argument is rather simple and is better demonstrated with我的论点相当简单,用

using std::sort;
using std::vector;

Imagine you have a code that contains a bug and now you are supposed to find it.想象一下,您有一个包含错误的代码,现在您应该找到它。 Any object or function used, that has a std:: in front is very very unlikely to contain a bug.任何使用的对象或函数,在前面有一个std::是非常不可能包含错误的。 Thus I always prefer to see a因此我总是喜欢看一个

std::vector<double> x;
std::sort(x);

instead of而不是

vector<double> x;
sort(x);

because the latter requires me to look up what vector and sort actually is (remember we are talking C++, ie it could be literally anything), just to find out that it is std::vector and std::sort .因为后者需要我查找vectorsort实际上是什么(记住我们说的是 C++,即它实际上可以是任何东西),只是为了找出它是std::vectorstd::sort Conclusion: The time I spend for writing std:: each and every time saves me double or more time for debugging.结论:我每次花在编写std::时间都为我节省了两倍或更多的调试时间。

To make it a bit less opinion based: The definite trade-off you have to make is readability vs less typing.使其不那么基于意见:您必须做出的明确权衡是可读性与更少的打字。 Opinion based is what you value more....基于意见的才是你更看重的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM