Re: Writing a .txt file
"Lucress Carol" <incognito.me@gmx.de> ha scritto nel messaggio
news:bdd11b16-56ba-4186-bbac-fef32b4195b8@d62g2000hsf.googlegroups.com...
When I write using namespace std; does it negativly affect
the performance?
Absolutely, not.
It does not negativly affect the performance.
"using namespace std" is just a compile-time thing (not a run-time thing).
I don't like that because I don't like to import *all* the standard library
classes into global namespace.
I prefer importing only selected classes, using explicit referencing, e.g.
using std::cout, using std::endl, etc.
Especially in big programs, it is very important to explicitly reference the
standard namespace std:: (e.g. std::cout, std::map, std::vector, etc.).
For example, if you do
using namespace std;
and you have some custom identifier in your code, called "map", this will
conflict with STL's map template class.
Thanks to std::map (std:: namespace identifier), you can disambiguate.
Namespaces are very important, especially in medium-big projects, to avoid
name conflicts.
void RandomNum(double small, double big ,int p, DoubleArray & array)
Why did you use a reference hier? you are let's say "copying" the
array
right?
I use reference there because the 'array' parameter is an *output*
parameter, I mean: your 'RandomNum' function writes data to that array
variable.
Passing by reference is kind of like passing by pointer (DoubleArray *), but
in C++ it's better to use references to pointers.
For example: pointers may be NULL, so you should check in your code that the
pointer is not NULL, using code like
ATLASSERT( pSomething != NULL );
before accessing the pointer.
Moreover, with references you can use simple dot notation (.), instead with
pointers you must use "arrow" notation (->).
// @@ a robust std::vector
DoubleArray array(3);
array[3] is meant to be the second element of array right?or
is it array.at(3)?
Keep in mind that "DoubleArray" is just a typedef for std::vector< double >.
So DoubleArray is just a particular std::vector.
std::vector array index is like raw C/C++ arrays, i.e.: it is 0-based.
So, array[0] is the 1st element, array[1] is the 2nd element, array[2] is
the 3rd element, and array[3] is the 4th element.
The difference between using [] vs. at() is that at() method does
bounds-checking, and it throws a C++ exception (I think std::out_of_range,
IIRC) if array index is out of bounds. Instead, at least in release builds,
operator[] does not throw anything.
So, if you want *super-fast* code (e.g. in a inner loop executed millions of
times...), you can use operator[], which gives performance like raw C
arrays, but does not do bounds checking.
Instead, for more *safe* code, use at() method (a little slower, because it
does bounds checking before accessing array element with input index).
A 2-dim vector would be for example:
DoubleArray array(3,2) and not array[3][2] right?
No. std::vector (remember: DoubleArray is a typedef for std::vector< double
>) supports only 1D arrays.
If you want 2D arrays you can build custom class to store them, using
std::vector (or std::valarray) as a "building block", or better you may want
to use a fast C++ library for matrix and vectors, like Blitz++:
http://www.oonumerics.org/blitz/
Blitz++ gives you support also for 2D matrixes (and also for higher order
tensors, IIRC). And it is *very* fast (it uses C++ template metaprogramming
techniques, but frankly speaking these techniques are above my head... Of
course, you don't need to master C++ template metaprogramming to just *use*
Blitz++).
HTH,
Giovanni