Reputation: 4764
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.
Upvotes: 4
Views: 4874
Reputation: 122298
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....
Upvotes: 3
Reputation: 234685
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.
Upvotes: 6