Reputation: 1046
I have a large 1D array of chars and I'm looking for a way to output the whole array without using the usual for-loop and doing this in a single output rather then thousands of outputs, the reason being performance and execution time reduction.
Is there anyway to be able to achieve this?
Upvotes: 3
Views: 19528
Reputation:
You can copy from the container to standard output.
For instance
using std::ostream_iterator;
using std::cout;
using std::begin;
using std::end;
using std::copy;
copy(arr.begin(), arr.end(), ostream_iterator<Type>(cout,”\n”)) ;
std::ostream_iterator
constructs output iterator tied to given ostream.
Upvotes: 0
Reputation: 10695
An alternative is to use ostream::write
which accepts a char array and the array size. The array is copied directly to the streambuffer, so it skips the formatting steps.
Edit
//since, std::cout is an ostream ...
std::cout.write( array, 8 );
Upvotes: 9
Reputation: 393064
If you don't mind taking a copy,
std::cout << std::string(a, a+N) << std::endl;
wrapping it into a generic function:
template <typename T, size_t N>
std::ostream& dump(std::ostream& os, const T (&a)[N])
{
return os << std::basic_string<T>(a, a+N) << std::endl;
}
// ....
wchar_t[] whello = L"Hello world";
dump(std::wcerr, whello) << std::endl;
unsigned char[] hello = "Hello world";
dump(std::cout, hello) << std::endl;
char buf[2048];
std::ofstream ofs("out.bin", std::ios::binary);
dump(ofs, buf);
Upvotes: 0
Reputation: 1474
To decrease execution time, if the characters have to be formatted, I would buffer the output to a std::ostringstream
and then copy the resulting string to the output.
Upvotes: 0
Reputation: 361462
As a general case, use std::copy
as:
T a[N]; //N is some constant!
//...
std::copy(a, a + N, std::ostream_iterator<T>(std::cout, " "));
Replace T
with char
, int
, std::string
or any type, it should work, as long as operator<<
is overloaded for T
, either as member function of std::ostream
or free function which takes std::ostream&
as first argument, and const T&
as second argument.
Upvotes: 5
Reputation: 101456
You can use the copy
algorithm from the Standard Library. If you want to output to the standard output, you send the contents to an ostream_iterator
on cout
:
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
char my_chars[] = {'a','b','c'};
copy( &my_chars[0], &my_chars[sizeof(my_chars)], ostream_iterator<char>(cout, "\t"));
}
You can also send the output to another stream, and then when that is done, dump the whole thing to cout
in one shot:
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <sstream>
using namespace std;
int main()
{
char my_chars[] = {'a','b','c'};
stringstream ss;
copy( &my_chars[0], &my_chars[sizeof(my_chars)], ostream_iterator<char>(ss, "\t"));
cout << ss.str() << endl;
}
Upvotes: 0
Reputation: 6671
// If it's null-terminated
#include <stdio.h>
printf( "%s\n", myarray );
Upvotes: 0