stdstring

forward declaration of std::string and std::wstring

天大地大妈咪最大 提交于 2019-12-23 12:05:10
问题 The problem of the inability to forward declare std::string and std::wstring is often discussed. As I understand, the reason is that those types are typedefing of instantiation of template class basic_string: namespace std { typedef basic_string<char> string; typedef basic_string<wchar_t> wstring; } And forward declaration of a typedef isn't allowed by the language. Wouldn't it be better for the c++ standard using inheritance instead of typedef: namespace std { class string : public basic

How to make Visual Studio 2010 warn about unused variables?

ⅰ亾dé卋堺 提交于 2019-12-23 09:34:25
问题 #include <string> using namespace std; int main() { string s; // no warning int i; // warning C4101 return 0; } Why does Visual Studio warn me about the unused variable i but not about s in the example? I assume that the compiler is not sure about side effects of the string constructor. Is this the reason for not showing a warning? Can I somehow enable warnings about unused string variables? My warning level is set to 4. 回答1: I hypothesize that compilers only warn about unused variables for

convert string to argv in c++

你说的曾经没有我的故事 提交于 2019-12-23 07:50:09
问题 I have an std::string containing a command to be executed with execv, what is the best "C++" way to convert it to the "char *argv[]" that is required by the second parameter of execv()? To clarify: std::string cmd = "mycommand arg1 arg2"; char *cmd_argv[]; StrToArgv(cmd, cmd_argv); // how do I write this function? execv(cmd_argv[0], cmd_argv); 回答1: std::vector<char *> args; std::istringstream iss(cmd); std::string token; while(iss >> token) { char *arg = new char[token.size() + 1]; copy(token

Is std::string::replace() optimized for same length strings?

放肆的年华 提交于 2019-12-22 10:55:04
问题 Suppose, most of the time I have below scenario for replacement: std::string line; // "line" contains a big string. std::string from = "abcd"; std::string to = "xy"; // to.length() < from.length() // replace "from" with "to" everywhere in "line" Here the string class has to put "xy" and then erase 2 characters which effectively shifts all character in line towards left. There are so many such replacements happening throughout the life of my code. Now coming to the real question. Below is also

Using a custom allocator in std::string to re-use an already allocated char buffer

杀马特。学长 韩版系。学妹 提交于 2019-12-22 04:09:26
问题 I need to use an already allocated char* buffer (with the string content) in a std::string object. After some research I found that this is almost impossible and std::string would have its own private copy of data always. The only remaining way I can think of to do this is to use a custom allocator that will return the address of the already allocated char buffer. For this to work, std::string should only use the allocator to allocate memory to hold its string data and for nothing else. Is

How to copy/set a volatile std::string?

那年仲夏 提交于 2019-12-21 20:34:18
问题 How can I copy a volatile std::string ? There is no copy constructor for volatile, nor does something like c_str allow volatile access. operator= also doesn't seem to allow setting a volatile. It seems like std::string is simply unusable as a volatile object. Is this intended, or am I missing some way to use it? NOTE: I have easy workarounds, I just came upon the issue while trying to use string in some low-level code. 回答1: As you noted, none of the member functions on std::string are marked

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

℡╲_俬逩灬. 提交于 2019-12-20 10:37:21
问题 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 wishes/constraints: It would be cool if it could run on limited hardware, such as computers with limited memory. I want the code to run on Windows, Mac and (if

What is the Linux equivalent of: MultiByteToWideChar & WideCharToMultiByte?

ε祈祈猫儿з 提交于 2019-12-20 10:29:30
问题 I am working with a class that wraps the std::wstring, this code needs to be cross platform, are there equivalents to the windows functions: MultiByteToWideChar & WideCharToMultiByte on linux? Thank you. 回答1: The Linux equivalents are the iconv functions iconv_open, iconv and iconv_close (say man 3 iconv_open etc. for the documentation). For cross-platform applications, use dedicated libraries such as ICU instead. Such libraries already contain their own string classes; there is no need to

Generating a SHA256 hash with Crypto++, using a string as input and output?

江枫思渺然 提交于 2019-12-20 09:36:48
问题 I need an example of how to use Crypto++ to generate a SHA256 hash from a std::string and output a std::string. I can't seem to figure it out. Everything I've tried gives me invalid output. Here's the new code after interjay's answer: string SHA256(string data) { byte const* pbData = (byte*) data.data(); unsigned int nDataLen = data.size(); byte abDigest[CryptoPP::SHA256::DIGESTSIZE]; CryptoPP::SHA256().CalculateDigest(abDigest, pbData, nDataLen); return string((char*)abDigest); } The output

C++ Is a std::string a container?

南笙酒味 提交于 2019-12-19 18:47:33
问题 This might be a simple question for some of you. But I was wondering if a std::string is a container. By container I mean the containers like for example std::vector , std::list and std::deque . Since std::basic_string<> accepts other types than integral characters, but also is being optimized by working with character arrays. It isn't really clear to me in which category it falls. This will compile: #include <string> #include <iostream> int main() { std::basic_string<int> int_str; int_str