stlstring

Why don't the std::fstream classes take a std::string?

This isn't a design question, really, though it may seem like it. (Well, okay, it's kind of a design question). What I'm wondering is why the C++ std::fstream classes don't take a std::string in their constructor or open methods. Everyone loves code examples so: #include <iostream> #include <fstream> #include <string> int main() { ...

How do you add an int to a string in C++?

int i = 4; string text = "Player "; cout << (text + i); I'd like it to cout "Player 4" ^ The above is obviously wrong but it shows what I'm trying to do here. Is there an easy way to do this or do I have to start adding new includes? ...

C++: how to get fprintf results as a std::string w/o sprintf

I am working with an open-source UNIX tool that is implemented in C++, and I need to change some code to get it to do what I want. I would like to make the smallest possible change in hopes of getting my patch accepted upstream. Solutions that are implementable in standard C++ and do not create more external dependencies are preferred. ...

C++ strings: UTF-8 or 16-bit encoding?

I'm still trying to decide whether my (home) project should use UTF-8 strings (implemented in terms of std::string with additional UTF-8-specific functions when necessary) or some 16-bit string (implemented as std::wstring). The project is a programming language and environment (like VB, it's a combination of both). There are a few wish...

How do you construct a std::string with an embedded null?

If I want to construct a std::string with a line like: std::string my_string("a\0b"); Where i want to have three characters in the resulting string (a, null, b), I only get one. What is the proper syntax? ...

bad_alloc error when using std::string

Hi all.. I'm currently working on a project that depends on me providing a path to a file (eg. "C:\Path.pth"). Now, I had everything working yesterday by calling my std::string with: std::string path("C:\\Path.pth"); -- But now it doesn't work!? It throws a bad_alloc. Seems like the '\' character is the problem. I even tried using \x5...

if(str1==str2) versus if(str1.length()==str2.length() && str1==str2)

I've seen second one in another's code and I suppose this length comparison have been done to increase code productivity. It was used in a parser for a script language with a specific dictionary: words are 4 to 24 letters long with the average of 7-8 lettets, alphabet includes 26 latin letters plus "@","$" and "_". Length comparison we...

C++ std::string Constructor

Hello all, any thoughts on this would be appreciated: std::string s1 = "hello"; std::string s2 = std::string(s1); I'd now expect these two strings to be independent, i.e. I could append ", world" to s2 and s1 would still read "hello". This is what I find on windows and linux but running the code on a HP_UX machine it seems that s2 and ...

C++ concatenate string and int

I thought this would be really simple but it's presenting some difficulties. If I have string name = "John"; int age = 21; How do I combine them to get a single string "John21"? ...

How do you convert a C++ string to an int?

How do you convert a C++ string to an int? Assume you are expecting the string to have actual numbers in it ("1", "345", "38944", for example). Also, let's assume you don't have boost, and you really want to do it the C++ way, not the crufty old C way. ...

What's the best way to trim std::string

I'm currently using the following code to right-trim all the std::strings in my programs: std::string s; s.erase(s.find_last_not_of(" \n\r\t")+1); It works fine, but I wonder if there are some end-cases where it might fail? Of course, answers with elegant alternatives and also left-trim solution are welcome. ...

Convert a number to a string with specified length in C++

I have some numbers of different length (like 1, 999, 76492, so on) and I want to convert them all to strings with a common length (for example, if the length is 6, then those strings will be: '000001', '000999', '076492'). In other words, I need to add correct amount of leading zeros to the number. int n = 999; string str = some_func...

Alternative to itoa() for converting integer to string C++?

I was wonding if there was an alternative to itoa() for converting an integer to a string because when I run it in visual Studio I get warnings, and when I compile my program under Linux, it won't even compile. Thanks, tomek ...

How to split a string?

What's the most elegant way to split a string in C++? The string can be assumed to be composed of words separated by whitespace. (Note that I'm not interested in C string functions or that kind of character manipulation/access. Also, please give precedence to elegance over efficiency in your answer.) The best solution I have right now ...

I want to convert std::string into a const wchar_t *

Is there any method? My computer is AMD64, ::std::string str; BOOL loadU(const wchar_t* lpszPathName, int flag = 0); when I used: loadU(&str); the VS2005 compiler says: Error 7 error C2664:: cannot convert parameter 1 from 'std::string *__w64 ' to 'const wchar_t *' How can I do it ? ...

Is std::string size() a O(1) operation?

Is std::string size() a O(1) operation? The implementation of STL I'm using is the one built into VC++ ...

c++ integer->std::string conversion. Simple function?

Problem: I have an integer; this integer needs to be converted to a stl::string type. In the past, I've used stringstream to do a conversion, and that's just kind of cumbersome. I know the C way is to do a sprintf, but I'd much rather do a C++ method that is typesafe(er). Is there a better way to do this? Here is the stringstream ap...