stdstring

Padding stl strings in C++

偶尔善良 提交于 2019-11-27 22:39:06
I'm using std::string and need to left pad them to a given width. What is the recommended way to do this in C++? Sample input: 123 pad to 10 characters. Sample output: 123 (7 spaces in front of 123) std::setw (setwidth) manipulator std::cout << std::setw (10) << 77 << std::endl; or std::cout << std::setw (10) << "hi!" << std::endl; outputs padded 77 and "hi!". if you need result as string use instance of std::stringstream instead std::cout object. ps: responsible header file <iomanip> void padTo(std::string &str, const size_t num, const char paddingChar = ' ') { if(num > str.size()) str.insert

convert standard C++ string to String^

家住魔仙堡 提交于 2019-11-27 19:38:30
问题 I want to convert to std::string to System::String^ in Visual C++ environment. I know that we can convert System::String to std::string by the MarshalString Function as below: void MarshalString ( String ^ s, string& os ) { using namespace Runtime::InteropServices; const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer(); os = chars; Marshal::FreeHGlobal(IntPtr((void*)chars)); } I can't find the way to convert std::string to System::String but I found that System::String

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

邮差的信 提交于 2019-11-27 18:50:33
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. Here is my problem. I have a C++ class -- let's call it "A" -- that currently uses fprintf() to print its heavily formatted data structures to a file pointer. In its print function, it also recursively calls the identically defined print functions of several

What are some algorithms for comparing how similar two strings are?

最后都变了- 提交于 2019-11-27 17:03:21
I need to compare strings to decide whether they represent the same thing. This relates to case titles entered by humans where abbreviations and other small details may differ. For example, consider the following two titles: std::string first = "Henry C. Harper v. The Law Offices of Huey & Luey, LLP"; As opposed to: std::string second = "Harper v. The Law Offices of Huey & Luey, LLP"; A human can quickly gauge that these are most likely one and the same. The current approach I have taken is to normalize the strings by lowercasing all letters and removing all punctuation and spaces giving: std:

Are end+1 iterators for std::string allowed?

谁说我不能喝 提交于 2019-11-27 14:32:04
问题 Is it valid to create an iterator to end(str)+1 for std::string ? And if it isn't, why isn't it? This question is restricted to C++11 and later, because while pre-C++11 the data was already stored in a continuous block in any but rare POC toy-implementations, the data didn't have to be stored that way. And I think that might make all the difference. The significant difference between std::string and any other standard container I speculate on is that it always contains one element more than

Reading directly from an std::istream into an std::string

廉价感情. 提交于 2019-11-27 13:42:00
Is there anyway to read a known number of bytes, directly into an std::string, without creating a temporary buffer to do so? eg currently I can do it by boost::uint16_t len; is.read((char*)&len, 2); char *tmpStr = new char[len]; is.read(tmpStr, len); std::string str(tmpStr, len); delete[] tmpStr; std::string has a resize function you could use, or a constructor that'll do the same: boost::uint16_t len; is.read((char*)&len, 2); std::string str(len, '\0'); is.read(&str[0], len); This is untested, and I don't know if strings are mandated to have contiguous storage. You could use a combination of

Legal to overwrite std::string's null terminator?

扶醉桌前 提交于 2019-11-27 12:36:18
In C++11, we know that std::string is guaranteed to be both contiguous and null-terminated (or more pedantically, terminated by charT() , which in the case of char is the null character 0). There is this C API I need to use that fills in a string by pointer. It writes the whole string + null terminator. In C++03, I was always forced to use a vector<char> , because I couldn't assume that string was contiguous or null-terminated. But in C++11 (assuming a properly conforming basic_string class, which is still iffy in some standard libraries), I can. Or can I? When I do this: std::string str

Concatenating strings doesn't work as expected [closed]

时光毁灭记忆、已成空白 提交于 2019-11-27 11:10:17
I know it is a common issue, but looking for references and other material I don't find a clear answer to this question. Consider the following code: #include <string> // ... // in a method std::string a = "Hello "; std::string b = "World"; std::string c = a + b; The compiler tells me it cannot find an overloaded operator for char[dim] . Does it mean that in the string there is not a + operator? But in several examples there is a situation like this one. If this is not the correct way to concat more strings, what is the best way? Your code, as written, works. You’re probably trying to achieve

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

ⅰ亾dé卋堺 提交于 2019-11-27 10:31:14
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 approach I have used in the past: std::string intToString(int i) { std::stringstream ss; std::string s; ss << i; s = ss.str(); return s; } Of course, this could be rewritten as so: template<class T> std::string t_to_string(T i) { std::stringstream ss; std::string s;

convert a char* to std::string

江枫思渺然 提交于 2019-11-27 10:03:44
I need to use an std::string to store data retrieved by fgets() . To do this I need to convert the char* return value from fgets() into an std::string to store in an array. How can this be done? std::string has a constructor for this: const char *s = "Hello, World!"; std::string str(s); Just make sure that your char * isn't NULL , or else the behavior is undefined. If you already know size of the char*, use this instead char* data = ...; int size = ...; std::string myString(data, size); This doesn't use strlen. EDIT: If string variable already exists, use assign(): std::string myString; char*