PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > [NEWBIE] from C string to std::string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
[NEWBIE] from C string to std::string

Réponse
 
LinkBack Outils de la discussion
Vieux 17/01/2008, 11h02   #1
Stefano Sabatini
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut [NEWBIE] from C string to std::string

Hi guys,
which is the best way to define a std::string using some C strings
(arrays of chars)?

Here it is the code:

#include <iostream>
#include <string>

using namespace std;

int main(void) {
string s;
// this won't even compile, since in this case "this is a" and
// others string are interpreted like char arrays
// s = "this is a" + " string.";

// OK, but awkward
s = string("this is a") + string(" string.");
cout << s;

// a better way??
// s = ...

return 0;
}

--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
  Réponse avec citation
Vieux 17/01/2008, 11h41   #2
Stefano Sabatini
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [NEWBIE] from C string to std::string

On 2008-01-17, Stefano Sabatini <stefano.sabatini@caos.org> wrote:
> Hi guys,
> which is the best way to define a std::string using some C strings
> (arrays of chars)?
>
> Here it is the code:
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main(void) {
> string s;
> // this won't even compile, since in this case "this is a" and
> // others string are interpreted like char arrays
> // s = "this is a" + " string.";
>
> // OK, but awkward
> s = string("this is a") + string(" string.");
> cout << s;
>
> // a better way??
> // s = ...
>
> return 0;
> }


Mmh..., it was very simple, reading another thread and FAQ-C++-lite
then I finally found a satisfying solution:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(void) {
string s;
// this won't work, since in this case "this" are interpreted like char arrays
// s = "this is a" + " string.";

// OK, but awkward
s = string("this is a") + string(" string");
cout << s;

// a better way??
std:stringstream o;
int i=-1;
o << "this is " << " a string " << "and this is a number: " << i << endl;
cout << o.str();

return 0;
}

for (int i=0; i < 10; i++)
cout << "regards ";
--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
  Réponse avec citation
Vieux 17/01/2008, 11h51   #3
anon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [NEWBIE] from C string to std::string

Stefano Sabatini wrote:
> On 2008-01-17, Stefano Sabatini <stefano.sabatini@caos.org> wrote:
>> Hi guys,
>> which is the best way to define a std::string using some C strings
>> (arrays of chars)?
>>
>> Here it is the code:
>>
>> #include <iostream>
>> #include <string>
>>
>> using namespace std;
>>
>> int main(void) {
>> string s;
>> // this won't even compile, since in this case "this is a" and
>> // others string are interpreted like char arrays
>> // s = "this is a" + " string.";
>>
>> // OK, but awkward
>> s = string("this is a") + string(" string.");
>> cout << s;
>>
>> // a better way??
>> // s = ...
>>
>> return 0;
>> }

>
> Mmh..., it was very simple, reading another thread and FAQ-C++-lite
> then I finally found a satisfying solution:
>
> #include <iostream>
> #include <string>
> #include <sstream>
>
> using namespace std;
>
> int main(void) {
> string s;
> // this won't work, since in this case "this" are interpreted like char arrays
> // s = "this is a" + " string.";
>
> // OK, but awkward
> s = string("this is a") + string(" string");
> cout << s;
>
> // a better way??
> std:stringstream o;
> int i=-1;
> o << "this is " << " a string " << "and this is a number: " << i << endl;
> cout << o.str();
>
> return 0;
> }


This doesn't corespond to what you asked, as you haven't mentioned
numbers - only char arrays. I expected something like this:

string s( "a very "
"long "
"string" );
  Réponse avec citation
Vieux 17/01/2008, 12h09   #4
Stefano Sabatini
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [NEWBIE] from C string to std::string

On 2008-01-17, anon <anon@no.no> wrote:
> Stefano Sabatini wrote:
>> On 2008-01-17, Stefano Sabatini <stefano.sabatini@caos.org> wrote:

[...]
>> Mmh..., it was very simple, reading another thread and FAQ-C++-lite
>> then I finally found a satisfying solution:
>>
>> #include <iostream>
>> #include <string>
>> #include <sstream>
>>
>> using namespace std;
>>
>> int main(void) {
>> string s;
>> // this won't work, since in this case "this" are interpreted like char arrays
>> // s = "this is a" + " string.";
>>
>> // OK, but awkward
>> s = string("this is a") + string(" string");
>> cout << s;
>>
>> // a better way??
>> std:stringstream o;
>> int i=-1;
>> o << "this is " << " a string " << "and this is a number: " << i << endl;
>> cout << o.str();
>>
>> return 0;
>> }

>
> This doesn't corespond to what you asked, as you haven't mentioned
> numbers - only char arrays. I expected something like this:
>
> string s( "a very "
> "long "
> "string" );


Hi, yes indeed, though what I was trying to ask for was if that was
possible to use a string in way similar to an iostream. sstream
addresses this requirement.

Thanks for your attention.

Regards.
--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
  Réponse avec citation
Vieux 17/01/2008, 17h45   #5
Linonut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [NEWBIE] from C string to std::string

* Stefano Sabatini peremptorily fired off this memo:

>> string s( "a very "
>> "long "
>> "string" );

>
> Hi, yes indeed, though what I was trying to ask for was if that was
> possible to use a string in way similar to an iostream. sstream
> addresses this requirement.
>
> Thanks for your attention.


Don't forget about:

s += "string value";

--
Intellect annuls Fate.
So far as a man thinks, he is free.
-- Ralph Waldo Emerson
  Réponse avec citation
Vieux 17/01/2008, 18h49   #6
Bo Persson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [NEWBIE] from C string to std::string

Stefano Sabatini wrote:
> Hi guys,
> which is the best way to define a std::string using some C strings
> (arrays of chars)?
>
> Here it is the code:
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main(void) {
> string s;
> // this won't even compile, since in this case "this is a" and
> // others string are interpreted like char arrays
> // s = "this is a" + " string.";


Adding two string literals together is done by the preprocessor. You
just have to put them adjacent to each other, WITHOUT any operators:

s = "this is a" " string.";


Of course, why would you want to do this? :-)


Bo Persson


  Réponse avec citation
Vieux 18/01/2008, 02h24   #7
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [NEWBIE] from C string to std::string

Stefano Sabatini wrote:
> Hi guys,
> which is the best way to define a std::string using some C strings
> (arrays of chars)?
>
> Here it is the code:
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main(void) {
> string s;
> // this won't even compile, since in this case "this is a" and
> // others string are interpreted like char arrays
> // s = "this is a" + " string.";
>
> // OK, but awkward
> s = string("this is a") + string(" string.");
> cout << s;
>
> // a better way??
> // s = ...
>
> return 0;
> }


Only the first string has to be converted to a std::string. Consider.

#include <string>

int main()
{
std::string s1;
s1 = "String1";
s1 += "string2";

std::string s2;
s2 = std::string("String1") + "String2";

std::string s3;
s3 = std::string("String1") + "String2" + "String3" + "String4";
}


--
Jim Langston
tazmaster@rocketmail.com


  Réponse avec citation
Vieux 22/01/2008, 11h49   #8
Richard Herring
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [NEWBIE] from C string to std::string

In message <5v9ma0F1kefs9U1@mid.individual.net>, Bo Persson <bop@gmb.dk>
writes
>Stefano Sabatini wrote:
>> Hi guys,
>> which is the best way to define a std::string using some C strings
>> (arrays of chars)?
>>
>> Here it is the code:
>>
>> #include <iostream>
>> #include <string>
>>
>> using namespace std;
>>
>> int main(void) {
>> string s;
>> // this won't even compile, since in this case "this is a" and
>> // others string are interpreted like char arrays
>> // s = "this is a" + " string.";

>
>Adding two string literals together is done by the preprocessor. You
>just have to put them adjacent to each other, WITHOUT any operators:
>
>s = "this is a" " string.";
>
>
>Of course, why would you want to do this? :-)
>


One reason might be to embed comments:

std::string angleExpression(
"^\\s*" /* skip leading whitespace */
"([-+]?)" /* exp1 is optional sign */
"\\s*" /* skip whitespace */
"(\\d*)" /* exp2 is integer degrees */
"([:nsew])" /* exp3 is punctuation, maybe quadrant letter */
"(\\d*)" /* exp4 is integer minutes */
"[:']" /* punctuator between min & sec */
"(\\d*)" /* exp5 is integer part of seconds */
"[.,]?" /* maybe there's a decimal part */
"(\\d*)" /* exp6 is fraction of seconds */
"[:"]?" /* may be final punctuation */
"\\s*" /* whitespace */
"([nsew]?)" /* exp7 is quadrant letter */
);

--
Richard Herring
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16488 seconds with 16 queries