Many strings, integers, floats etc.. in one string

Hello.
I have many strings like bellow without order, general in random...

(1," bit is the minimum amount of information that we can imagine"," since it only stores either value ",1," or ",0,", which represents either YES or NO, activated")

How i can put all these stuff in a string? I don't want many functions for each case.
Last edited on
I did not understand.
Since no one has replied, you should assume that it is a bit difficult to understand what you mean
If I understand correctly, you want to store a CSV record. Use a std::stringstream.

1
2
#include <iostream>
#include <sstream> 
1
2
3
4
5
6
7
8
9
ostringstream ss;
ss << 1;
ss << "," << " bit is the minimum amount of information that we can imagine";
ss << "," << " since it only stores either value ";
ss << "," << 1;
ss << "," << " or ";
ss << "," << 0;
ss << "," << "\"" << ", which represents either YES or NO, activated" << "\"";
string result = ss.str();

This produces the following string:

1, bit is the minimum amount of information that we can imagine, since it only stores either value ,1, or ,0,", which represents either YES or NO, activated"


If you simply want all the stuff concatenated into a single string, it works just like using cout:

1
2
3
4
5
6
7
8
9
ostringstream ss;
ss << 1;
ss << " bit is the minimum amount of information that we can imagine";
ss << " since it only stores either value ";
ss << 1;
ss << " or ";
ss << 0;
ss << ", which represents either YES or NO, activated";
string result = ss.str();

Which produces the following string:

1 bit is the minimum amount of information that we can imagine since it only stores either value 1 or 0, which represents either YES or NO, activated

Hope this helps.
Hello.
The string and values stuff is not the same each time. I am looking for a function that will take different numbers of strings, integers, doubles and maybe structures and from all those stuff will give a string...
I don't know how i can start thinking for strings ant integers for first place.
For example ...
make_string("The string and values",1,2," stuff is not the same each time."," I am looking for a function that ") for example.
The type and place each time are different ...

Any idea?
You can do this two ways. The way I recommend is Template Metaprogramming, but you will need a compiler that supports C++11 Parameter Packs.

The alternative is to make a function like printf using VA Lists, but you will have to have a first parameter that gives information about the rest of the parameters.
closed account (Dy7SLyTq)
i thought for metaprogramming it had to be constant size at compile time? and i think he wants it to be variable. anyways my best guess would be string stream but you would need to use variable size argument lists like LB said
DTSCode wrote:
i thought for metaprogramming it had to be constant size at compile time? and i think he wants it to be variable.
He wants it to be variable at compile time.
Can't pass objects that are not trivially copyable or references as variadic function arguments.
Ideally avoid them in C++ code.

C++98:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <sstream>
#include <complex>

struct stringify
{
    operator std::string() const { return str ; }
    std::string str ;
};

template < typename T > stringify operator+ ( stringify s, const T& object )
{
    std::ostringstream stm ;
    stm << object ;
    if( !s.str.empty() && !std::isspace( *s.str.rbegin() ) ) s.str += ' ' ;
    s.str += stm.str() ;
    return s ;
}

int main()
{
    std::complex<double> c( 1.23,3.45) ;

    const std::string str = stringify() + "The string and values" + 1 + 2 +
                            " stuff is not the same each time." + '\n' + c +
                            5.789 + " I am looking for a function that " + '\n' ;

    std::cout << str ;
}

http://coliru.stacked-crooked.com/a/b0dfee56bd474254
1
2
3
4
std::ostringstream ss;
ss << "I'm not sure (" << true << ") how this is different " << 3.141592 
   << " from anything " << some_fn() << " else, so far." << '\n';
std::cout << ss.str();

Can OP give us an example of what he is trying to do? Use a std::tuple (heterogeneous list)? Convert one to a string (human-readable text version)?
Hello, i just go in.

Can OP give us an example of what he is trying to do?

I have different solutions for problems and must give each solution as a string to not have many functions make_string with referent parameter's.
So i must use a way to solve this.
In mind that problems are more than a 1000.
Can you imagine how many make_string must have make, if a problem have 2 or 3 solutions?

Last edited on
Dear JLBorges thank you.
Your code is amazing.
Takes all stuff and gives string.
Thanh's

Jim

@Duoas: Sometimes people want a different way to do the same thing for no reason.
Hello @Duoas:
Sorry maybe you have right.
Your way has ss variable. Imaging that i have thousand of problems and solutions.
I must have same number of variables ss...
How i can do that without writing vars?





Last edited on
> Your way has ss variable. Imaging that i have thousand of problems and solutions.
> I must have same number of variables ss...

No, just one stream std::ostringstream object would suffice.

1
2
3
4
5
6
7
8
9
std::ostringstream ss;

ss << a << b << c << d << e ; 
const std::string str = ss.str() ;

// when we want to use it again
ss.str("") ; // clear the stringbuf
ss << f << g << h  ; 
const std::string str2 = ss.str() ;


Duoas's way is the straightforward, simple, way of doing what you want.
(My post was really about not using C-style variadic functions, even in C++98).
Thanh's very much @Duoas && JLBorges.
I just open my pc.

Jim
Topic archived. No new replies allowed.