473,321 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Stringifying a vector

Hello,

Another thread some time ago showed how to print a container like this:

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. void display_container( const T& v ) {
  3. // display elements using method of choice
  4. std::copy(v.begin(), v.end(), std::ostream_iterator<typename
  5. T::value_type>(std::cout, "\n"));
  6. }
  7.  
I'm wondering how I would send it to a string instead. I tried:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4.  
  5. elements-vector
  6. template <class T>
  7. inline std::string display_container( const T& v ) 
  8. {
  9.     std::string result;
  10.     // display elements using method of choice
  11.     std::copy(v.begin(), v.end(), std::ostream_iterator<typename T::value_type>(result, "\n"));
  12.     return result;
  13. }
  14.  

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?
Sep 14 '09 #1
9 4010
Banfa
9,065 Expert Mod 8TB
Try using a stringstream instead of a string.
Sep 14 '09 #2
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:

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. inline std::string display_container( const T& v ) 
  3. {
  4.     std::stringstream temp;
  5.     std::string result;
  6.     // display elements using method of choice
  7.     std::copy(v.begin(), v.end(), std::ostream_iterator<typename T::value_type>(temp, "\n"));
  8.     result << temp;
  9.     return(result);
  10. }
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:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. inline std::string display_container( const T& v ) 
  3. {
  4.     std::stringstream temp;
  5.     std::string result;
  6.     // display elements using method of choice
  7.     std::copy(v.begin(), v.end(), std::ostream_iterator<typename T::value_type>(temp, "\n"));
  8.     result = temp.rdbuf();
  9.     return(result);
  10. }
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.
Sep 15 '09 #3
Savage
1,764 Expert 1GB
Did you include <string> ?

Also stream are usually on the left side,not variables, did you try this instead:

temp >> result;

,of result << temp?
Sep 15 '09 #4
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();
Sep 15 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
Use a stringstream.

You can use the << operatir to put things in your stringstream (since it's a stream):

Expand|Select|Wrap|Line Numbers
  1. string x("hello");
  2.  
  3. stringstream ss;
  4.  
  5. ss << x;
Then you can use the >> operator to get things out of a stringstream:

Expand|Select|Wrap|Line Numbers
  1. ss >> x;
Your code looks OK but you need an operator<< and an operator>> for the class you put into the stringstream.
Sep 15 '09 #6
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:

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <sstream>
  3.  
  4. // display_container() taken from http://bytes.com/topic/c/answers/133231-print-elements-vector
  5. template <class T>
  6. inline std::string display_container( const T& v ) 
  7. {
  8.     std::stringstream result;
  9.     // display elements using method of choice
  10.     std::copy(v.begin(), v.end(), std::ostream_iterator<typename T::value_type>(result, "\n"));
  11.     return result.str();
  12. }

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
Sep 15 '09 #7
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
Sep 15 '09 #8
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.
Sep 16 '09 #9
Savage
1,764 Expert 1GB
@columbo124
With great power comes great responsibility :P

,kind regards

Savage
Sep 16 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
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...
9
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...
7
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...
34
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...
10
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;
8
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...
16
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:...
6
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...
24
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...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
1
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.