stdstring

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

流过昼夜 提交于 2019-12-17 15:37:49
问题 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_function(n,6); //str = '000999' Is there a function like this in C++? 回答1: or using the stringstreams: #include <sstream> #include <iomanip> std::stringstream ss; ss

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

孤街醉人 提交于 2019-12-17 10:55:11
问题 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; 回答1: 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

Explicit copy constructor

一个人想着一个人 提交于 2019-12-17 07:39:46
问题 I have extended std::string to fulfil my needs of having to write custom function build into string class called CustomString I have defined constructors: class CustomString : public std::string { public: explicit CustomString(void); explicit CustomString(const std::string& str); explicit CustomString(const CustomString& customString); //assignment operator CustomString& operator=(const CustomString& customString); ... }; In the third constructor (copy constructor) and assignment operator,

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

ぐ巨炮叔叔 提交于 2019-12-17 07:17:13
问题 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? 回答1: If you have a std::wstring object, you can call c_str() on it to get a wchar_t* : std::wstring name( L"Steve Nash" ); const wchar_t* szName = name.c_str(); Since you are operating on a narrow string, however,

How to get the number of characters in a std::string?

回眸只為那壹抹淺笑 提交于 2019-12-17 05:41:00
问题 How should I get the number of characters in a string in C++? 回答1: If you're using a std::string , call length(): std::string str = "hello"; std::cout << str << ":" << str.length(); // Outputs "hello:5" If you're using a c-string, call strlen(). const char *str = "hello"; std::cout << str << ":" << strlen(str); // Outputs "hello:5" Or, if you happen to like using Pascal-style strings (or f***** strings as Joel Spolsky likes to call them when they have a trailing NULL), just dereference the

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

大憨熊 提交于 2019-12-17 03:31:41
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How to parse a string to an int in C++? 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. 回答1: #include <sstream> // st is input string int result; stringstream(st) >> result; 回答2: Use the C++ streams. std:

C++ std::basic_string/char_traits specialization

倾然丶 夕夏残阳落幕 提交于 2019-12-14 03:12:59
问题 This is related to: std::basic_string specialization and Circumventing template specialization I tried the solution from std::basic_string specialization, but the problem is that CustomChar is a typedef for wchar_t and I have redefinition(conflict with specialization from std). How may I avoid usage of chart_traits when I'm not allowed to change CustomChar typedef? 回答1: You could use Boost's/C++0x's disable_if to disable if there are known previous instantiations, like in the case of char and

How to convert an std::string to C-style string [duplicate]

走远了吗. 提交于 2019-12-13 18:37:01
问题 This question already has answers here : How to convert a std::string to const char* or char*? (8 answers) Closed 3 years ago . I am programming in C++. As basic as this question is I cannot seem to find an answer for it anywhere. So here is the problem: I want to create a C-style string however I want to put an integer variable i into the string. So naturally, I used a stream: stringstream foo; foo << "blah blah blah blah... i = " << i << " blah blah... "; However, I need to somehow get a C

How to capitalize a word in a C++ string?

送分小仙女□ 提交于 2019-12-13 16:44:40
问题 I have a std::string and wish for the first letter to be capitalized and the rest lower case. One way I could do this is: const std::string example("eXamPLe"); std::string capitalized = boost::to_lower_copy(example); capitalized[0] = toupper(capitalized[0]); Which would yield capitalized as: "Example" But perhaps there is a more straight forward way to do this? 回答1: If the string is indeed just a single word, std::string capitalized = boost::locale::to_title (example) should do it. Otherwise,

How to remove consonants from a string? [closed]

天大地大妈咪最大 提交于 2019-12-13 10:54:43
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last year . Here is my code: #include <iostream> #include <string> #include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while(n--) { string str; char a[] = {'a','e','i','o','u','A','E','I','O','U'}; getline(cin, str); for(int i=0 ;i<str.length(); i++) { for(int j=0; j<10; j++) { if(str[i]=