3

I searched this site and people says you should avoid using using namespace std. I totally agree. However, what about using std::cin and using 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.

However, when you type using std::cin etc in the begining of the file, it seems very crowd. For example, this simple program read and calculate student grade, in front of it, there are too many using std::, it look very uncomfortable.

#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:

Remember, there is nothing wrong with using 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.

sohnryang
  • 725
  • 2
  • 13
  • 27
an offer can't refuse
  • 4,245
  • 5
  • 30
  • 50

2 Answers2

6

Never use 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. Learn to love the more verbose std:: notation.

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • You should vote to close as POB if all the OP is going to get is Opinions. – NathanOliver Oct 05 '16 at 13:09
  • I believe the first paragraph is not opinion based. – Bathsheba Oct 05 '16 at 13:09
  • if you are writing a simple hello world or tutorial type program, it helps to **using namespace std** so long as no other library has cin or cout as types. No need to complicate tutorials with std::this std::that .... – Dr Deo Oct 05 '16 at 13:21
  • Thank for your advice, I think I should raise a good habit from the beginning. – an offer can't refuse Oct 05 '16 at 13:22
  • 1
    @DrDeo: I couldn't disagree more. I think the fact that textbooks seem to despise writing `std::` is the cause of this. It incepts that style into the minds of all our beginner programmers! – Bathsheba Oct 05 '16 at 13:22
  • @DrDeo one could (or should?) argue that there is no need to introduce bad habits in tutorials without any good reason (other than lazyness) – 463035818_is_not_an_ai Oct 05 '16 at 13:25
  • `std::` is such a huge namespace with so many commonly used names that I would still recommend avoiding it in `.cpp` files, it can cause hard to find bugs even in begginer, example type problems. – Galik Oct 05 '16 at 13:36
3

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. 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. Conclusion: The time I spend for writing std:: each and every time saves me double or more time for debugging.

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

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • I think `coding-style` questions are all opinion based, however there are good ones and bad ones. Besides, almost all the code are opinion based, because code is the way to **the** answer, however there are many ways. – an offer can't refuse Oct 05 '16 at 13:21
  • @buzhidao as I said, what imho is not opinion based is the advantage and disadvantges of one approach vs the other. Which one you choose is purely opinion based – 463035818_is_not_an_ai Oct 05 '16 at 13:23