Hello, Another thread some time ago showed how to print a container like this: -
template <class T>
-
void display_container( const T& v ) {
-
// display elements using method of choice
-
std::copy(v.begin(), v.end(), std::ostream_iterator<typename
-
T::value_type>(std::cout, "\n"));
-
}
-
I'm wondering how I would send it to a string instead. I tried: -
#include <iostream>
-
#include <sstream>
-
#include <fstream>
-
-
elements-vector
-
template <class T>
- inline std::string display_container( const T& v )
-
{
-
std::string result;
-
// display elements using method of choice
-
std::copy(v.begin(), v.end(), std::ostream_iterator<typename T::value_type>(result, "\n"));
-
return result;
-
}
-
But I appear to get a type error:
error: no matching function for call to ‘std::ostream_iterator<int, char, std::char_traits<char> >::ostream_iterator(std::basic_string<char, std::char_traits<char>,
Any way to patch this up?
9 4010
Closer, but I still get an error. I had tried this (that's why that #include <sstring> was in the first example). But the error I'm getting is much less scary: -
template <class T>
-
inline std::string display_container( const T& v )
-
{
-
std::stringstream temp;
-
std::string result;
-
// display elements using method of choice
-
std::copy(v.begin(), v.end(), std::ostream_iterator<typename T::value_type>(temp, "\n"));
-
result << temp;
-
return(result);
-
}
now produces:
In file included from BisectionSearch.cpp:4:
/usr/include/rectra.h: In function ‘std::string display_container(const T&)’:
/usr/include/rectra.h:32: error: no match for ‘operator<<’ in ‘result << temp’
I also tried: -
template <class T>
-
inline std::string display_container( const T& v )
-
{
-
std::stringstream temp;
-
std::string result;
-
// display elements using method of choice
-
std::copy(v.begin(), v.end(), std::ostream_iterator<typename T::value_type>(temp, "\n"));
-
result = temp.rdbuf();
-
return(result);
-
}
but this produces:
/usr/include/rectra.h: In function ‘std::string display_container(const T&) [with T = std::vector<int, std::allocator<int> >]’:
BisectionSearch.cpp:43: instantiated from here
/usr/include/rectra.h:32: error: invalid conversion from ‘std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >*’ to ‘char’
/usr/include/rectra.h:32: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’
I'm sure I'm close, but can't quite get there.
Did you include <string> ?
Also stream are usually on the left side,not variables, did you try this instead:
temp >> result;
,of result << temp?
Banfa 9,065
Expert Mod 8TB
You should read the documentation a bit more carefully, it has an example. Did you try
result = temp.str();
Use a stringstream.
You can use the << operatir to put things in your stringstream (since it's a stream): - string x("hello");
-
-
stringstream ss;
-
-
ss << x;
Then you can use the >> operator to get things out of a stringstream:
Your code looks OK but you need an operator<< and an operator>> for the class you put into the stringstream.
I'm experienced in C, but not C++.
Ok, so having stubbed my toe on each leg of the table in turn, I cut the program down to the minimum reproducible size and am compiling that on its own as rectra2.cpp as follows: - #include <string>
-
#include <sstream>
-
-
// display_container() taken from http://bytes.com/topic/c/answers/133231-print-elements-vector
-
template <class T>
-
inline std::string display_container( const T& v )
-
{
-
std::stringstream result;
-
// display elements using method of choice
-
std::copy(v.begin(), v.end(), std::ostream_iterator<typename T::value_type>(result, "\n"));
-
return result.str();
-
}
Which then produces:
a$ gcc -c -ggdb -fPIC -Wall -orectra2 rectra2.cpp
rectra2.cpp: In function ‘std::string display_container(const T&)’:
rectra2.cpp:10: error: ‘ostream_iterator’ is not a member of ‘std’
rectra2.cpp:10: error: expected `(' before ‘>’ token
Thanks for that.
I think our messages crossed while I was away from the computer.
The idea was to get the text of the container into a string without
having to write a operator<< or operator>> for each one. Using an
extract from the other thread referenced above had some problems:
1. I started with code that only used << or >> but no copy() function
and this wouldn't process arrays. (It would, but it just printed the address).
2. This code that I'm working on in this topic also comes from that thread which handles containers well without having to overload operators, but only sends it to cout, not a string. I'm trying to add this functionality.
The current minimal program is posted previously. @weaknessforcats
Awright, fooey! #include <iterator>
Ok, I figured it out.
In PERL, you concentrate on getting things done.
In C++, you concentrate on semantics, templates, classes,
hierarchies, header files, templates, trying and catching errors, not using macros, templates, iterators, inheritance,
polymorphism, templates, whether it should be C-style or
C++-style, whether is C-style or C++-style, and
getting things done.
@columbo124
With great power comes great responsibility :P
,kind regards
Savage
Sign in to post your reply or Sign up for a free account.
Similar topics
by: {AGUT2}=IWIK= |
last post by:
Hello all,
It's my fisrt post here and I am feeling a little stupid here, so go easy..
:) (Oh, and I've spent _hours_ searching...)
I am desperately trying to read in an ASCII...
|
by: luigi |
last post by:
Hi,
I am trying to speed up the perfomance of stl vector by
allocating/deallocating blocks of memory manually. one version of the
code crashes when I try to free the memory. The other version...
|
by: Forecast |
last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully.
: // proj2.cc: returns a dynamic vector and prints out at main~~
: //
: #include <iostream>
: #include <vector>
:
: using...
|
by: Adam Hartshorne |
last post by:
Hi All,
I have the following problem, and I would be extremely grateful if
somebody would be kind enough to suggest an efficient solution to it.
I create an instance of a Class A, and...
|
by: Bob |
last post by:
Here's what I have:
void miniVector<T>::insertOrder(miniVector<T>& v,const T& item)
{
int i, j;
T target;
vSize += 1;
T newVector;
newVector=new T;
|
by: Ross A. Finlayson |
last post by:
I'm trying to write some C code, but I want to use C++'s std::vector.
Indeed, if the code is compiled as C++, I want the container to
actually be std::vector, in this case of a collection of value...
|
by: Martin Jørgensen |
last post by:
Hi,
I get this using g++:
main.cpp:9: error: new types may not be defined in a return type
main.cpp:9: note: (perhaps a semicolon is missing after the definition
of 'vector')
main.cpp:9:...
|
by: zl2k |
last post by:
hi, there
I am using a big, sparse binary array (size of 256^3). The size may be
changed in run time. I first thought about using the bitset but found
its size is unchangeable. If I use the...
|
by: toton |
last post by:
Hi,
I want to have a vector like class with some additional functionality
(cosmetic one). So can I inherit a vector class to add the addition
function like,
CorresVector : public...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |