C++ Standard Library: How to write wrappers for cout, cerr, cin and endl?
Why not
using std::cin;
using std::cout;
and so on? Then in your code you can use cin
, cout
, and so on, without accidentally injecting all of the rest of the std
namespace into your code.
Should `using std::cin` and `using std::cout` be avoided or encouraged?
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.
C++ String Class Input
cout << fruity[i] << endl;
This is invalid. Do this instead:
for(i = 0; i < SIZE; i++)
cout << fruity[i] << endl;
Because at that point your i
will be equal to SIZE
, and fruity[SIZE]
is invalid.
How do I print out the contents of a vector?
If you have a C++11 compiler, I would suggest using a range-based for-loop (see below); or else use an iterator. But you have several options, all of which I will explain in what follows.
Range-based for-loop (C++11)
In C++11 (and later) you can use the new range-based for-loop, which looks like this:
std::vector<char> path;
// ...
for (char i: path)
std::cout << i << ' ';
The type char
in the for-loop statement should be the type of the elements of the vector path
and not an integer indexing type. In other words, since path
is of type std::vector<char>
, the type that should appear in the range-based for-loop is char
. However, you will likely often see the explicit type replaced with the auto
placeholder type:
for (auto i: path)
std::cout << i << ' ';
Regardless of whether you use the explicit type or the auto
keyword, the object i
has a value that is a copy of the actual item in the path
object. Thus, all changes to i
in the loop are not preserved in path
itself:
std::vector<char> path{'a', 'b', 'c'};
for (auto i: path) {
i = '_'; // 'i' is a copy of the element in 'path', so although
// we can change 'i' here perfectly fine, the elements
// of 'path' have not changed
std::cout << i << ' '; // will print: "_ _ _"
}
for (auto i: path) {
std::cout << i << ' '; // will print: "a b c"
}
If you would like to proscribe being able to change this copied value of i
in the for-loop as well, you can force the type of i
to be const char
like this:
for (const auto i: path) {
i = '_'; // this will now produce a compiler error
std::cout << i << ' ';
}
If you would like to modify the items in path
so that those changes persist in path
outside of the for-loop, then you can use a reference like so:
for (auto& i: path) {
i = '_'; // changes to 'i' will now also change the
// element in 'path' itself to that value
std::cout << i << ' ';
}
and even if you don't want to modify path
, if the copying of objects is expensive you should use a const reference instead of copying by value:
for (const auto& i: path)
std::cout << i << ' ';
Iterators
Before C++11 the canonical solution would have been to use an iterator, and that is still perfectly acceptable. They are used as follows:
std::vector<char> path;
// ...
for (std::vector<char>::const_iterator i = path.begin(); i != path.end(); ++i)
std::cout << *i << ' ';
If you want to modify the vector's contents in the for-loop, then use iterator
rather than const_iterator
.
Supplement: typedef / type alias (C++11) / auto (C++11)
This is not another solution, but a supplement to the above iterator
solution. If you are using the C++11 standard (or later), then you can use the auto
keyword to help the readability:
for (auto i = path.begin(); i != path.end(); ++i)
std::cout << *i << ' ';
Here the type of i
will be non-const (i.e., the compiler will use std::vector<char>::iterator
as the type of i
). This is because we called the begin
method, so the compiler deduced the type for i
from that. If we call the cbegin
method instead ("c" for const), then i
will be a std::vector<char>::const_iterator
:
for (auto i = path.cbegin(); i != path.cend(); ++i) {
*i = '_'; // will produce a compiler error
std::cout << *i << ' ';
}
If you're not comfortable with the compiler deducing types, then in C++11 you can use a type alias to avoid having to type the vector out all the time (a good habit to get into):
using Path = std::vector<char>; // C++11 onwards only
Path path; // 'Path' is an alias for std::vector<char>
// ...
for (Path::const_iterator i = path.begin(); i != path.end(); ++i)
std::cout << *i << ' ';
If you do not have access to a C++11 compiler (or don't like the type alias syntax for whatever reason), then you can use the more traditional typedef
:
typedef std::vector<char> Path; // 'Path' now a synonym for std::vector<char>
Path path;
// ...
for (Path::const_iterator i = path.begin(); i != path.end(); ++i)
std::cout << *i << ' ';
Side note:
At this point, you may or may not have come across iterators before, and you may or may not have heard that iterators are what you are "supposed" to use, and may be wondering why. The answer is not easy to appreciate, but, in brief, the idea is that iterators are an abstraction that shield you from the details of the operation.
It is convenient to have an object (the iterator) that does the operation you want (like sequential access) rather than you writing the details yourself (the "details" being the code that does the actual accessing of the elements of the vector). You should notice that in the for-loop you are only ever asking the iterator to return you a value (*i
, where i
is the iterator) -- you are never interacting with path
directly itself. The logic goes like this: you create an iterator and give it the object you want to loop over (iterator i = path.begin()
), and then all you do is ask the iterator to get the next value for you (*i
); you never had to worry exactly how the iterator did that -- that's its business, not yours.
OK, but what's the point? Well, imagine if getting a value wasn't simple. What if it involves a bit of work? You don't need to worry, because the iterator has handled that for you -- it sorts out the details, all you need to do is ask it for a value. Additionally, what if you change the container from std::vector
to something else? In theory, your code doesn't change even if the details of how accessing elements in the new container does: remember, the iterator sorts all the details out for you behind the scenes, so you don't need to change your code at all -- you just ask the iterator for the next value in the container, same as before.
So, whilst this may seem like confusing overkill for looping through a vector, there are good reasons behind the concept of iterators and so you might as well get used to using them.
Indexing
You can also use a integer type to index through the elements of the vector in the for-loop explicitly:
for (int i=0; i<path.size(); ++i)
std::cout << path[i] << ' ';
If you are going to do this, it's better to use the container's member types, if they are available and appropriate. std::vector
has a member type called size_type
for this job: it is the type returned by the size
method.
typedef std::vector<char> Path; // 'Path' now a synonym for std::vector<char>
for (Path::size_type i=0; i<path.size(); ++i)
std::cout << path[i] << ' ';
Why not use this in preference to the iterator
solution? For simple cases, you can do that, but using an iterator
brings several advantages, which I have briefly outlined above. As such, my advice would be to avoid this method unless you have good reasons for it.
std::copy (C++11)
See Joshua's answer. You can use the STL algorithm std::copy
to copy the vector contents onto the output stream. I don't have anything to add, except to say that I don't use this method; but there's no good reason for that besides habit.
std::ranges::copy (C++20)
For completeness, C++20 introduced ranges, which can act on the whole range of a std::vector
, so no need for begin
and end
:
#include <iterator> // for std::ostream_iterator
#include <algorithm> // for std::ranges::copy depending on lib support
std::vector<char> path;
// ...
std::ranges::copy(path, std::ostream_iterator<char>(std::cout, " "));
Unless you have a recent compiler (on GCC apparently at least version 10.1), likely you will not have ranges support even if you might have some C++20 features available.
Overload std::ostream::operator<<
See also Chris's answer below. This is more a complement to the other answers since you will still need to implement one of the solutions above in the overloading, but the benefit is much cleaner code. This is how you could use the std::ranges::copy
solution above:
#include <iostream>
#include <vector>
#include <iterator> // for std::ostream_iterator
#include <algorithm> // for std::ranges::copy depending on lib support
using Path = std::vector<char>; // type alias for std::vector<char>
std::ostream& operator<< (std::ostream& out, const Path& v) {
if ( !v.empty() ) {
out << '[';
std::ranges::copy(v, std::ostream_iterator<char>(out, ", "));
out << "\b\b]"; // use two ANSI backspace characters '\b' to overwrite final ", "
}
return out;
}
int main() {
Path path{'/', 'f', 'o', 'o'};
// will output: "path: [/, f, o, o]"
std::cout << "path: " << path << std::endl;
return 0;
}
Now you can pass your Path
objects to your output stream just like fundamental types. Using any of the other solutions above should also be equally straightforward.
Conclusion
Any of the solutions presented here will work. It's up to you (and context or your coding standards) on which one is the "best". Anything more detailed than this is probably best left for another question where the pros/cons can be properly evaluated, but as always user preference will always play a part: none of the solutions presented are objectively wrong, but some will look nicer to each coder.
Addendum
This is an expanded solution of an earlier one I posted. Since that post kept getting attention, I decided to expand on it and refer to the other excellent solutions posted here, at least those that I have personally used in the past at least once. I would, however, encourage the reader to look at the answers below because there are probably good suggestions that I have forgotten, or do not know, about.
How to make compiler work out template class arguments at assigmnet?
In C++11 you can use auto
to do that:
auto Three=Multiply(One,Two);
In current C++ you cannot do this.
One way to avoid having to spell out the type's name is to move the code dealing with Three
into a function template:
template< int a, int b >
void do_something_with_it(const Matrix<a,b>& One, const Matrix<a,b>& Two)
{
Matrix<a,b> Three = Multiply(One,Two);
// ...
}
int main()
{
Matrix<2,3> One;
One.print();
Matrix<3,5> Two;
do_something_with_it(One,Two);
return 0;
}
Edit: A few more notes to your code.
- Be careful with
using namespace std;
, it can lead to very nasty surprises. - Unless you plan to have matrices with negative dimensions, using
unsigned int
or, even more appropriate,std::size_t
would be better for the template arguments. - You shouldn't pass matrices per copy. Pass per const reference instead.
Multiply()
could be spelledoperator*
, which would allowMatrix<2,3> Three = One * Two;
print
should probably take the stream to print to asstd::ostream&
. And I'd prefer it to be a free function instead of a member function. I would contemplate overloadingoperator<<
instead of naming itprint
.
Custom C++ cout class - output to both console and log file
The sync
calls can be replaced with pubsync
. As for the overflow
call I think that may be a typo. as it looks as if it should be a call to sputc
.
C++ Why won't my sample program create the output file?
Because you're trying to open the file for reading:
outFile.open(outputFile.c_str(), ios::in);
Use ios::out
instead.
How to use an iterator?
That your code compiles at all is probably because you have a using namespace std
somewhere. (Otherwise vector
would have to be std::vector
.) That's something I would advise against and you have just provided a good case why:
By accident, your call picks up std::distance()
, which takes two iterators and calculates the distance between them. Remove the using directive and prefix all standard library types with std::
and the compiler will tell you that you tried to pass a vector <point>::iterator
where a point*
was required.
To get a pointer to an object an iterator points to, you'd have to dereference the iterator - which gives a reference to the object - and take the address of the result: &*ii
.
(Note that a pointer would perfectly fulfill all requirements for a std::vector
iterator and some earlier implementations of the standard library indeed used pointers for that, which allowed you to treat std::vector
iterators as pointers. But modern implementations use a special iterator class for that. I suppose the reason is that using a class allows overloading functions for pointers and iterators. Also, using pointers as std::vector
iterators encourages mixing pointers and iterators, which will prevent the code to compile when you change your container.)
But rather than doing this, I suggest you change your function so that it takes references instead (see this answer for why that's a good idea anyway.) :
float distance(const point& p1, const point& p2)
{
return sqrt((p1.x - p2.x)*(p1.x - p2.x) +
(p1.y - p2.y)*(p1.y - p2.y));
}
Note that the points are taken by const
references. This indicates to the caller that the function won't change the points it is passed.
Then you can call it like this: distance(*ii,*jj)
.
On a side note, this
typedef struct point {
float x;
float y;
} point;
is a C-ism unnecessary in C++. Just spell it
struct point {
float x;
float y;
};
That would make problems if this struct
definition ever was to parse from a C compiler (the code would have to refer to struct point
then, not simply point
), but I guess std::vector
and the like would be far more of a challenge to a C compiler anyway.
Related Topics
How to Convert Wstring into String
Is It Good Practice to Null a Pointer After Deleting It
Variadic Template Pack Expansion
C++ Cross-Platform High-Resolution Timer
How to Detect Whether There Is a Specific Member Variable in Class
How Does the Import Library Work - Details
Unmangling the Result of Std::Type_Info::Name
How to Detect Reliably MAC Os X, Ios, Linux, Windows in C Preprocessor
What Happens When I Print an Uninitialized Variable in C++
What Happens to a Detached Thread When Main() Exits
How to Convert an Enum Type Variable to a String
C and C++: Partial Initialization of Automatic Structure
Linux API to List Running Processes
How to Pass a Unique_Ptr Argument to a Constructor or a Function
What's the Difference Between Assignment Operator and Copy Constructor