stdstring

C++ variable arguments with std::string only

寵の児 提交于 2019-12-10 13:34:16
问题 I'm trying to create a function that takes a variable amount of std::string arguments and formats a string with it. Example: Test::formatLine(const string::format, ...) { const std::string buffer; va_list args; va_start(args, format); vsprintf(buffer.c_str, format.c_str, args); va_end(args); cout << buffer << endl; } Compiling this snippet errors: Error 1 error C3867: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str': function call missing argument list; use '&std:

Why does emplace_back(“Hello”) call strlen?

*爱你&永不变心* 提交于 2019-12-10 12:53:07
问题 Justin's answer on another question made an observation that I find very interesting but can't quite explain. Consider the following code: std::vector<std::string> v; v.push_back("Hello, world!"); // Doesn't call strlen. v.emplace_back("Hello, world!"); // Calls strlen. If you look at the assembly, emplace_back generates a call to strlen, whereas push_back does not (tested in gcc 8.1 and clang 6.0 using -Ofast ). Why is this happening? Why can't emplace_back optimize out the strlen call here?

Default advice for using C-style string literals vs. constructing unnamed std::string objects?

北慕城南 提交于 2019-12-10 12:35:35
问题 So C++ 14 introduced a number of user-defined literals to use, one of which is the "s" literal suffix, for creating std::string objects. According to the documentation, its behavior is exactly the same as constructing an std::string object, like so: auto str = "Hello World!"s; // RHS is equivalent to: std::string{ "Hello World!" } Of course constructing an unnamed std::string object could be done prior to C++ 14, but because the C++ 14 way is so much simpler, I think way more people will

How to efficiently copy a std::string into a vector

北战南征 提交于 2019-12-10 10:55:33
问题 I have a string std::string s = "Stack Overflow"; That I need to copy into a vector. This is how I am doing it std::vector<char> v; v.reserve(s.length()+1); for(std::string::const_iterator it = s.begin(); it != s.end(); ++it) { v.push_back( *it ); } v.push_back( '\0' ); But I hear range operation are more efficient. So I am thinking something like this std::vector<char> v( s.begin(), s.end()); v.push_back('\0'); But is this better in this case? What about the potential re-allocation when

std::string as a key in std::map using a compare operator

我的梦境 提交于 2019-12-10 10:25:47
问题 I'm trying to use a std::string as a key in a std::map however, i'm unable to find() correctly. My code is somewhat complicated and large so this is a small program that demonstrates the problem I'm having. If someone could tell me why this doesn't work, i'd be very grateful. Thanks. #include <stdio.h> #include <string> #include <map> struct comparer { public: bool operator()(const std::string x, const std::string y) { return x.compare(y)==0; } }; int main(int argc, char *argv[]) { std::map

Misuse of GL info log null terminating character in std::string?

大兔子大兔子 提交于 2019-12-10 10:13:08
问题 I have a fairly simple log() method for a GL shader and program convenience classes, since the respective compile and link methods only return a bool , while hiding all the GL calls; e.g., std::string glShader::log () const { std::string info; GLint len = 0; if (!glIsShader(gl_shader_obj)) info = "(invalid shader object)\n"; else glGetShaderiv(gl_shader_obj, GL_INFO_LOG_LENGTH, & len); if (len != 0) { info.resize(static_cast<std::string::size_type>(len)); glGetShaderInfoLog(gl_shader_obj, len

Is it possible to use an std::string for read()?

 ̄綄美尐妖づ 提交于 2019-12-10 03:11:24
问题 Is it possible to use an std::string for read() ? Example : std::string data; read(fd, data, 42); Normaly, we have to use char* but is it possible to directly use a std::string ? (I prefer don't create a char* for store the result) Thank's 回答1: Well, you'll need to create a char* somehow, since that's what the function requires. (BTW: you are talking about the Posix function read , aren't you, and not std::istream::read ?) The problem isn't the char* , it's what the char* points to (which I

STL basic_string length with null characters

喜欢而已 提交于 2019-12-10 03:04:01
问题 Why is it that you can insert a '\0' char in a std::basic_string and the .length() method is unaffected but if you call char_traits<char>::length(str.c_str()) you get the length of the string up until the first '\0' character? e.g. string str("abcdefgh"); cout << str.length(); // 8 str[4] = '\0'; cout << str.length(); // 8 cout << char_traits<char>::length(str.c_str()); // 4 回答1: Great question! The reason is that a C-style string is defined as a sequence of bytes that ends with a null byte.

Test whether libstdc++'s version uses a C++11-compliant std::string

喜欢而已 提交于 2019-12-10 01:50:31
问题 I'm writing some C++11 code that makes assumptions about the nature of std::string that are valid, but represent behavior that was changed in C++11. In the earlier days, libstdc++'s basic_string implementation conformed to the 98/03 requirements, but not to the more strict C++11 requirements. As I understand it, libstdc++ has fixed the issues around basic_string . The problem is that there are many versions of the library that people use which do not implement this fix. And my code may

How to Fix Visual Studio 2012 error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string?

爷,独闯天下 提交于 2019-12-10 01:50:26
问题 How to fix a Visual Studio 2012 error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string? I've been compiling a solution containing one exe and several static lib projects on which the exe depends fine with Visual Studio 2008. The static libs include: TCL: tcl84tsx.lib wxWidgets: wxbase.lib zlib.lib ws2_32.lib xerces-c_2.lib SNMP Research EMANATE: subagent.lib,agent.lib,emanate.lib,pack.lib,mibtable.lib,devkit.lib etc. I loaded the solution into Visual