Hello,
I think I have asked this question before,but I couldn't find it in my
thread collections.
My question is
sprintf (name, "str%ld", ++(*i));
How do I convert i with str%ld to "name" when it is of type std::string
rather than char *?
i is int*

Please let me know if you got the answer.
Thanks in advance

Re: string conversions with std::string and int * by SvenC

SvenC
Sat Jun 23 05:15:15 CDT 2007

Hi,

> Hello,
> I think I have asked this question before,but I couldn't find it in my
> thread collections.
> My question is
> sprintf (name, "str%ld", ++(*i));
> How do I convert i with str%ld to "name" when it is of type
> std::string rather than char *?
> i is int*

name << "str" << *i;

--
SvenC

Re: string conversions with std::string and int * by Jack

Jack
Sat Jun 23 05:30:38 CDT 2007


"SvenC" <SvenC@community.nospam>
???????:4F70B5D5-1B0E-4636-9295-6D632F4C6620@microsoft.com...
> Hi,
>
>> Hello,
>> I think I have asked this question before,but I couldn't find it in my
>> thread collections.
>> My question is
>> sprintf (name, "str%ld", ++(*i));
>> How do I convert i with str%ld to "name" when it is of type
>> std::string rather than char *?
>> i is int*
>
> name << "str" << *i;

Error 19 error C2678: binary '<<' : no operator found which takes a
left-hand operand of type 'std::string' (or there is no acceptable
conversion)

:(
I have included <string>, did i just miss something?
Thanks
Jack

>
> --
> SvenC



Re: string conversions with std::string and int * by Alex

Alex
Sat Jun 23 06:19:46 CDT 2007

"Jack" wrote:
>> name << "str" << *i;
>
> Error 19 error C2678: binary '<<' : no operator found
> which takes a left-hand operand of type 'std::string' (or
> there is no acceptable conversion)
>
> :(
> I have included <string>, did i just miss something?

You need to unclude <strstream> and declare `name' as
`strstream':

#include <string>
#include <strstream>

...

using namespace std;

strstream name;
name << "str" << *i;

string s1 = name.str();


Alex


Re: string conversions with std::string and int * by Jack

Jack
Sat Jun 23 06:30:54 CDT 2007


"Alex Blekhman" <xfkt@oohay.moc>
???????:e3IVphYtHHA.4424@TK2MSFTNGP04.phx.gbl...
> "Jack" wrote:
>>> name << "str" << *i;
>>
>> Error 19 error C2678: binary '<<' : no operator found which takes a
>> left-hand operand of type 'std::string' (or there is no acceptable
>> conversion)
>>
>> :(
>> I have included <string>, did i just miss something?
>
> You need to unclude <strstream> and declare `name' as `strstream':
>
> #include <string>
> #include <strstream>
>
> ...
>
> using namespace std;
>
> strstream name;
> name << "str" << *i;
>
> string s1 = name.str();
Dear Alex,
It works perfectly. Thanks for your help!
Jack
>
>
> Alex
>



Re: string conversions with std::string and int * by Mateusz

Mateusz
Sat Jun 23 08:29:44 CDT 2007

On Sat, 23 Jun 2007 12:08:03 +0200, Jack <jl@knight.com> wrote:
>
> My question is
> sprintf (name, "str%ld", ++(*i));
> How do I convert i with str%ld to "name" when it is of type std::strin=
g
> rather than char *?
> i is int*

1. using <sstream>

http://mateusz.loskot.net/2006/08/17/save-formatted-data-into-stdstring/=


2. using Boost Format

#include <boost/format.hpp>
int main()
{
int i =3D 101;
std::string name;

name =3D str(boost::format("str%d") % i);

std::cout << name << std::endl;

return 0;
}

Cheers
-- =

Mateusz Loskot
http://mateusz.loskot.net