I sometimes see people code like std::cin and stuff and say that it's bad to use
"using namespace std;"
Why?.....
A C++ namespace is not simply extra syntax and/or overhead, it's a system/mechanism within the language that is designed to prevent name collisions. The std namespace is enormous, every compiler-provided variable/constant/function is contained within it.
When you say using namespace std;
, you make all of those names visible to your program. If all of those names are visible you can't, for example, declare your own "string" class without getting a compiler error related to redefining the string class. The error occurs because C++ has a "string" class that is part of the std namespace. If you don't pull in the entire std namespace, and only pull in what you need, the std::string class isn't visible and there is no longer a naming conflict.
In addition to what Fbody mentions, including an entire namespace can hide subtle errors in code (especially for beginners). Consider the following:
#include <iostream>
#include <algorithm>
using namespace std;
int main () {
int x = 5, y = 3;
cout << "Before swap: " << x << " " << y << endl;
swap (x, y);
cout << "After swap: " << x << " " << y << endl;
return 0;
}
void swap (int& x, int& y) {
x = 1000;
y = 4000;
}
An inexperienced programmer may expect that to change the value of x and y to 1000 and 4000 respectively. However, since there was no declaration of the local version of swap
and there just happens to be a swap
defined in the std
namespace there is no compilation warning/error and the result is actually something other than expected (try it yourself).
This is a silly example, but it illustrates a subtlety about namespaces and what can happen.
In addition to what Fbody mentions, including an entire namespace can hide subtle errors in code (especially for beginners). Consider the following:
#include <iostream> #include <algorithm> using namespace std; int main () { int x = 5, y = 3; cout << "Before swap: " << x << " " << y << endl; swap (x, y); cout << "After swap: " << x << " " << y << endl; return 0; } void swap (int& x, int& y) { x = 1000; y = 4000; }
An inexperienced programmer may expect that to change the value of x and y to 1000 and 4000 respectively. However, since there was no declaration of the local version of
swap
and there just happens to be aswap
defined in thestd
namespace there is no compilation warning/error and the result is actually something other than expected (try it yourself).This is a silly example, but it illustrates a subtlety about namespaces and what can happen.
So one should write like std::cin and such?
Seems annoying / ugly =(
Should I be doing that? (std::), do experienced programmers do that? =(
also I don't quite understand your swap function >.<
Shouldn't you do swap(&x, &y) iny our main function?
and i see people do int *pointer = &x;
Why? why not just write &x when u need it's memory address?
>.<
So one should write like std::cin and such?
Seems annoying / ugly =(
Should I be doing that? (std::), do experienced programmers do that? =(
Yes, you can write like that. I have read a few C++ books and both have been shown. I suppose it comes down to preference, as I am a limited C++ programmer myself, but what you may do instead is sometime similar to this.
/*
Purpose: thread1529731
Name: Saith
Date: 4/8/11
*/
#include<iostream>
int main() {
using namespace std;
int x;
cin >> x;
cout << x;
return 0;
}
The previous code will only use the
using namespace std;
in that block of code so you can continue to use cin without the need of std::cin; but for that block only, if you try to use cin in any other function without the leading std:: the compiler will give an error. Likewise, you may put that namespace in any other block of code and will be limited to that block only.
also I don't quite understand your swap function >.<
Shouldn't you do swap(&x, &y) iny our main function?
There is a swap function in the std. The compiler will use the std function swap() that takes two arguments and swaps the values for each.
The only time you need to use the & for call-by-reference is when you are -declaring- a function and when you are -defining- a function, you -do not- need the & when calling the function.
and i see people do int *pointer = &x;
Why? why not just write &x when u need it's memory address?
The pointer is not needed in the previous example. You can use the pointer as an iterator in certain situations that require you to move up and down memory (e.g. array) or possibly a linked list. If you try to change the address of where you are currently at with &x, you will be hard coding memory addresses which should be avoided.
It's not necessarily the need of a memory address, it's the need to move around memory easier.
>.<
:D
So one should write like std::cin and such?
Seems annoying / ugly =(
Should I be doing that? (std::), do experienced programmers do that? =(
I prefer the explicit std::
version when I code. However, you can also do something like:
using std::cin;
using std::cout;
int main () {
int x;
cin >> x;
cout << x;
// ...
}
And only call in the items you intend to use.
Just to be clear: there is nothing drastically wrong with using namespace std
. It is important to understand what it does so that you can understand how to look for (and avoid) problems related to doing that.
>>The only time you need to use the & for call-by-reference is when you are -declaring- a function and when you are -defining- a function, you -do not- need the & when calling the function.
This is only partially true. There are 2 ways to "call by reference". There is an actual reference parameter, and then there is the pointer parameter. For the library function std::swap() this statement is true because it's declared to use reference parameters, not pointers, but you should be careful about making all-encompasing statements like this.
Whether you need the '&' or not depends on the declaration of the function, and the specific types of arguments expected. If you are using reference parameters, the '&' is not necessary in the argument, but it may be required in the argument if you are using pointer parameters.
Passing using reference parameters:
#include <iostream>
using std::cout;
using std::endl;
void setTen(int &);
int main() {
int myValue = 0;
cout << "myValue is: " << myValue << endl;
setTen(myValue); //call setTen(), notice no '&'
cout << "myValue is now: " << myValue << endl;
return 0;
}
void setTen(int &theValue) {
cout << "in setTen()...\nsetting theValue to 10..." << endl;
theValue = 10;
}
Passing using pointer parameter and reference argument:
#include <iostream>
using std::cout;
using std::endl;
void setTen(int *);
int main() {
int myValue = 0;
cout << "myValue is: " << myValue << endl;
setTen(&myValue); //call setTen(), notice use of '&'
cout << "myValue is now: " << myValue << endl;
return 0;
}
void setTen(int *theValue) {
cout << "in setTen()...\nsetting *valuePtr to 10..." << endl;
*valuePtr = 10;
}
Passing using pointer parameter and pointer argument:
#include <iostream>
using std::cout;
using std::endl;
void setTen(int *);
int main() {
int myValue = 0;
int *pValue = &myValue;
cout << "myValue is: " << myValue << endl;
setTen(pValue); //call setTen(), notice no '&'
cout << "myValue is now: " << myValue << endl;
return 0;
}
void setTen(int *valuePtr) {
cout << "in setTen()...\nsetting *valuePtr to 10..." << endl;
*valuePtr = 10;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.