std::string = char* + std::string#include <string>
int main ()
{
std::string MyString = "Testing";
MyString = " " + MyString;
}
This works in Microsoft Visual C++ .net 2003
The end result being MyString contans the text " Testing"
I'm wondering if this is UB. Apparently MSVC is converting
the " " to a std::string, then doing the +. Otherwise it would
try to add a std::string to a char* which would fail.
I know I can do MyString = std::string(" ") + MyString; and
all would still work. But I'm wondering if I have to (to stay
within the standards...
from std::string to std::istream?Can I convert a std::string to a std::istream object?
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
On 2005-03-17, Gernot Frisch <Me@Privacy.net> wrote:
> Can I convert a std::string to a std::istream object?
The question doesn't even make sense. What are you trying to do ?
Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
"Gernot Frisch" <Me@Privacy.net> wrote...
> Can I convert a std::string to a std::istream object?
No. But you can create an std::istringstream object with it:
std::string mystring("1 2 3");
std::istringstream is(mystring);
int one, two, three;
is >> one >> two >> three;
Of course, my effort is wasted if that's not what you wanted. Next
time please be more verbose trying to explain what you need instead
of asking about legality of a potential solution to your task.
V
"Victor Bazarov" <v.Abazarov@comAcast.net> schrieb im Newsbeitrag
news:nPqdnXLo7cBwMqTfRVn-pQ@comcast.com...
> "Gernot Frisch" <Me@Privacy.net> wrote...
>> Can I convert a std::string to a std::istream object?
>
> No. But you can create an std::istringstream object with it:
>
> std::string mystring("1 2 3");
> std::istringstream is(mystring);
> int one, two, three;
> is >> one >> two >>...
How to extract an std::string from another std::string?Suppose I have an std::string called line that has the value
"sdsder>abc<gd<< sds". I want to get the substring between the greater-
than less-than signs and assign it to another std::string called
substr. How do I do this? My guess is that you should use two string
iterators, one called it_begin that points to the first occurrence of
a greater-than sign and another string iterator called it_end that
starts at the same spot as it_begin and goes to the first occurrence
of a less-than sign. Then you somehow extract the data between those
two iterators. Here's what I have so far:
#include <iostream>
#include <string>
int main()
{
std::string line = "sdsder>abc<gd<< sds";
std::string::iterator it_begin;
std::string::iterator it_end;
return 0;
}
Here's where I'm stumped. What value should I be assigning these two
iterators? Obviously their indexes need to be six and ten, but we need
to assume that line could be any string at all, not the garbage one
given. How do I tell std::string to "find the first occurrence of
character such-and-such and point it there"?
This is a MIME GnuPG-signed message. If you see this text, it means that
your E-mail or Usenet software does not support MIME signed messages.
The Internet standard for MIME PGP messages, RFC 2015, was published in 1996.
To open this message correctly you will need to install E-mail or Usenet
software th...
std::string and std::wostreamHi,
I was wondering why there was no predefine operator<< puting a
std::string into a std::wostream. The strangest is that something
similar to this would work:
std::wostream& operator<<(std::wostream& stream, std::string str)
{
stream << str.c_str();
return stream;
}
The only problem with this solution is it invalidate the different
iterators you have on the string (because of the call to c_str), while
the operator<< should not do that.
Thanks,
Pierre
Pierre Barbier de Reuille wrote:
> Hi,
>
> I was wondering why there was no predefine oper...
Q: Convert std::string to std::wstring using std::ctype widen()Hi All,
I've done a little homework (I've read responses to similar from P.J.
Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
is what I am performing (Stroustrup's Appendix D recommendation won't
compile in Microsoft VC++ 6.0).
My question is in reference to MultiByte Character Sets. Will this code
perform as expected? I understand every problem has a simple and
elegant solution that is wrong.
I generally use US English or Unicode, so I don't encounter a lot of
issues others may see (a multibyte character using std::string). I have
verified it work...
Extracting an std::string from within another std::stringSuppose I have an std::string called data that I know has at least one
greater-than sign and one less-than sign following it. I want to
extract data's value to just the letters inside the greater-than and
less-than signs. For example, if data = "a c>123<DDR" I want to change
data to just "123". How do I do this? I'm trying to assign an
std::iterator called it_begin that is set to the first occurrence of a
greater-than sign, and another iterator called it_end that is set to
the first occurrence of a less-than sign, then call data =
data.substr(it_begin,...
std::string::push_back vs. std::string::operator+=Hi,
I just browsed libstdc++6-doc and stumbled over a string operation I haven't
noticed before: string::push_back.
If for example I want to append single characters to an std::string, which
one would be better, or is there any difference at all:
mystr.push_back( c );
or
mystr += c;
Any ideas? I used to use the latter one.
Regards,
Matthias
Matthias K�ppler wrote in news:cntj5d$ooa$03$1@news.t-online.com in
comp.lang.c++:
> Hi,
>
> I just browsed libstdc++6-doc and stumbled over a string operation I
> haven't noticed before: string::push_back.
push_back() is there so that std::string conforms as "Back
Insertion Sequence".
Because its there you can use std::back_inserter with std::string
for example. Or any other generic algorithm that requires a back
insertion sequence.
> If for example I want to append single characters to an std::string,
> which one would be better, or is there any difference at all:
>
> mystr.push_back( c );
>
> or
>
> mystr += c;
>
> Any ideas? I used to use the latter one.
>
Whichever you want, they both do the same thing.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
> I just browsed libstdc++6-doc and stumbled over a string operation I haven't
> noticed before: string::push_back.
> If for example I want to append single characters to an std::string, which
> one would be better, or is there any difference at all:
>
> mystr.push_back( c );
>
...
Solaris Studio C++ std::cout std::string core dump under multithread environment.
Hi,
Recently I meet a very strange core dump issue and I investigate
this issue for long time and I have some ideas, however I have no idea
it is correct or not. So I want some guys could give me some
suggestions or tips, thanks a lot.
The environment: Solaris X86 64bit, Solaris Studio C++ 12 (Sun Studio
C++) with -mt -pthread and libCstd.so, and use libumem library.
The core dump point is as follows:
// ci is a int, cn and sv is a string
// and I am sure ci, cn and sv is only function local variables and
they
// aren't shared by multithreads
cout<<" ...
std::any and std::exception_ptrN3804 proposes the introduction of std::any.
Now, C++ already has an "any"-like type, namely std::exception_ptr. Semantically, after the introduction of std::any, exception_ptr will be a special case of std::any, namely a semantical std::any initialized by a caught exception.
Wouldn't it make sense to make these two interoperate nicely? In particular,
** std::exception_ptr should be able to be converted into a std::any, as, currently, the only way of looking into an exception_ptr is re-throwing and catching immediately (please correct me if I am wrong).
** std::rethro...
std::string::npos always < std::string::size() ?Is std::string::npos always going to be less than any std::string 's size()?
I am trying to handle a replacement of all occurances of a substr, in which
the replacement also contains the substr. Yick. All I could come up with is:
#include <string>
int main()
{
std::string text;
text = "\nThis is a test line with newlines\n<-here and \n<-here and
\n\n<-two here";
// Format the text
std::string formattedText(text);
// Change every occurrance of "\n" to "\r\n"
std::string::size_type index = 0;
do
{
index = form...
std::any and std::exception_ptr{ edited by mod to shorten lines to ~70 characters. -mod }
[mistakenly posted to comp.lang.c++ before]
N3804 proposes the introduction of std::any.
Now, C++ already has an "any"-like type, namely std::exception_ptr.
Semantically, after the introduction of std::any, exception_ptr will be
a special case of std::any, namely a semantical std::any initialized by
a caught exception.
Wouldn't it make sense to make these two interoperate nicely? In
particular,
** std::exception_ptr should be able to be converted into a std::any,
as,
currently, the only way of look...
std::string and std::ostringstream performancesHi,
I have a C++ application that extensively uses std::string and
std::ostringstream in somewhat similar manner as below
std::string msgHeader;
msgHeader = "<";
msgHeader += a;
msgHeader += "><";
msgHeader += b;
msgHeader += "><";
msgHeader += c;
msgHeader += ">";
Similarly it uses ostringstream as well and the function that uses
this gets called almost on every message that my application gets on
the socket. I am using this to precisely construct a XML Message to
be sent to another application.
What we observed when we ran a c...
Question about std:string and std:coutI am a beginner of C++;
I have a question about the std:string and std:cout class;
Two pieces of code:
--------------------------------
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "Hii";
string s2 = "MSDN Fans!\n";
cout<<s1+s2;
}
---------------------------------
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "Hii";
string s2 = "MSDN Fans!\n";
string s3 = s1 + s2;
cout<< s3;
}
Which one would be run faster or they'r totally the same....
converting std::string to std::wstringI have a an app that I'm writing which uses char and std::string. I'm using
a library which expects wchar_t arrays.
Is there a standard way to convert between std::string and std::wstring, or do
I need to use something like std::transform()?
Thanks
"red floyd" <no.spam@here.dude> wrote in message news:I3ejb.539$0g6.117@newssvr29.news.prodigy.com...
> I have a an app that I'm writing which uses char and std::string. I'm using
> a library which expects wchar_t arrays.
>
> Is there a standard way to convert between std::string and std::wstring, o...
How to convert std::string to std::istream?
I have a function that use std::istream as parameter. Right now I need
to pass a std::string into it, how can I convert std::string to
std::istream type?
Thanks
Water Lin
--
Water Lin's notes and pencils: http://en.waterlin.org
Email: WaterLin@ymail.com
Water Lin wrote:
> I have a function that use std::istream as parameter. Right now I need
> to pass a std::string into it, how can I convert std::string to
> std::istream type?
You can't directly, but you can use a stringstream:
#include <iostream>
#include <sstream>
void f( std::istream& in )
{
std::string s;
in >> s;
std::cout << s << std::endl;
}
int main()
{
std::string s("hello");
std::istringstream ss( s );
f( ss );
}
--
Ian Collins
Ian Collins <ian-news@hotmail.com> writes:
> Water Lin wrote:
>> I have a function that use std::istream as parameter. Right now I need
>> to pass a std::string into it, how can I convert std::string to
>> std::istream type?
>
> You can't directly, but you can use a stringstream:
>
> #include <iostream>
> #include <sstream>
>
> void f( std::istream& in )
> {
> std::string s;
> in >> s;
> std::cout << s << std::endl;
> }
>
> int main()
> {
> std::string s("hello");
> std::istringstream ss( s );
>
> f( ss );
> }
Thanks, I got it.
Water Lin
--
Water Lin&...
std::map<int,std::set<std::string> > Wrong? (Segmentation fault.)Hello,
I have the following code:
std::map<int,std::set<std::string> > k;
k[0]="1234567890";
k[1]="2345678901";
//...
std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)
throw(std::runtime_error)
{
std::map<int,std::set<std::string> >::const_iterator i;
i=k.find(0);
if(i==k.end())
throw std::runtime_error("No zero in k.");
return i->second;
}
Compilation of this code goes well, but I have the following problem while
executing this in my implementation: "Segmentation fault". I have
pi...
which headers to #includeSuppose I have a program which uses 'ostream' type as the parameter
type for a function and 'cout' object for writing into the standard
output. Following is my understanding regarding, which headers to
#include for this scenario.
Since the program uses 'cout' object, I have to #include <iostream>
header for 'cout'. In the ISO/IEC-14882:2003 document, in page 608, in
section 27.3 - Standard iostream objects, the synopsis for the header
<iostream> mentions the following line(apart from other statements):
extern ostream cout;
From the ab...
using std::string; string("hello") vs std::string("hello") in header file.In Accellerated C++, the author recommends that in a header file one should
not declare
using std::string, using std::vector etc instead one should directly specify
the namespace specifier in code.
for example, this is bad practice:
header.h
#include <string>
using std::string;
class a{
string x;
};
instead, one should write:
#include <string>
class a{
std::string x;
};
The reason given by the author is that 'using std::string' actually pollutes
the scope where 'using std::string' is declared.
I find this style is rather inconvenient when I want to experiment with
different implementation of certain classes in large projects. For example,
I may want to try myns::string or john::string, it's actually much easier to
replace 'using std::string' to 'using myns::string' and instantly switch to
another string implementation. This is much better than going through all
the instances of 'std::string variable' and replace them to 'myns::string
variable'.
What are your thoughts on this coding style?
Fei Liu wrote:
> In Accellerated C++, the author recommends that in a header file one
> should
> not declare
> using std::string, using std::vector etc instead one should directly
> specify
> the namespace specifier in code.
Doesn't it say "at top level"?
> The reason given by the author is that 'using std::string' actually
> pollutes
> the scope where 'using...
std::bitset construction with std::string questionCan anyone tell me why
std::bitset<2> foo(std::string("01"))
initializes the bitset in reverse order, i.e.
foo[0]=true
foo[1]=false
I would expect the bitset to be initialized in string text order.
"Dill Hole" <dillhole@microsoft.com> wrote in message
news:7brbgvs98ibjn2fcdtl7drc7tk82fstpoj@4ax.com...
> Can anyone tell me why
>
> std::bitset<2> foo(std::string("01"))
>
> initializes the bitset in reverse order, i.e.
>
> foo[0]=true
> foo[1]=false
>
> I would expect the bitset to be initialized in string text or...
std::fstream::seekp( ... std::ios::end )Hi there,
I think I misunderstood the purpose of std::fstream::seekp. I would
like to append data at the end of a file. So I wrote the following
piece of code (*).
It prints '0' in my console, which means the call to seekp(0,
std::ios::end) did not seek to the end of the file. How am I supposed
to do that in a portable way ? (no such thing as std::ios::app)
Thanks,
#include <fstream>
#include <iostream>
int main()
{
const unsigned int len = 512;
char buffer1[len] = {};
char buffer2[len] = {};
{
std::ofstream of( "dummy.raw", std::ios::binary );
...
Question about std:string & std:coutI am a beginner of C++;
I have a question about the std:string and std:cout class;
Two pieces of code:
--------------------------------
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "Hii";
string s2 = "MSDN Fans!\n";
cout<<s1+s2;
}
---------------------------------
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "Hii";
string s2 = "MSDN Fans!\n";
string s3 = s1 + s2;
cout<< s3;
}
Which one would be run faster or they'r totally the same....
binary write of std::string and std::vector
Hi,
I need to make binary write/read of std::string and std::vector
to a file (by using ofstream/ifstream). Is there any quick way of
doing this, or one has to write/read the contents element by element
by using ofstream::write(buffer,size)?
L.B.
!!! PLEASE NOTE MY NEW ADDRESS SINCE January 1st, 2008, INDICATED BELOW !!!
*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Complex Systems and Chemical ...
std::stringstream && std::stringHello!
I am using std::stringstream && std::string to parse a text file ->
std::ifstream in;
std::string s;
std::streamstring ss;
int n;
I grab each line like so -> std::getline(in,s);
I then do this to get the string into a stringstream -> ss << s;
I am using a std::stringstream because it makes extracting data easier
like so -> ss >> n;
Is this the proper way to use std::stringstream? How do I blank out
the std::stringstream object because each time I do a (ss << s;) the
stream keeps concatenating onto itself...how do I blank out the stream
to ad...
std::vector<char> instead of std::string, where are the string searching functions?I am working with some legacy code that is in the process of changing
to use std::vector<char> instead of a C-style char array. The C-style
char array is currently allocated using new char [n]. This array is
passed to various C string functions such as strstr, strncmp etc. I
need to do the same work but with a std::vector. I googled around for
a bit to see if I could find anyone who had already done this work but
my search revealed nothing. I wonder if some kind person could point
me in the right direction.
Now, I realise I could code it all myself but surely there must be
something out there where this has already been done. I would rather
build on the work of others than re-invent the wheel. And for
performance critical apps such as the one I am working on, it is
common advice to use std::vector<char> instead of std::string or C-
style char arrays. In the past I often seen this advice given out
(it's even in More Effective STL) but without the utility functions to
back it up I can see people ignoring this advice.
FWIW, the app is reading in sections of a *huge* XML file. A buffer is
used to hold a fragment which is then parsed using the Xerces SAX
parser (thus it avoids creating a DOM object). I want the buffer to be
a std::vector<char> that sometimes expands to reach a new watermark. I
think I've got that bit working but the string compares fail coz it
goes off the end of the vector.
Regards,
Andrew Marlow
--
...