stdstring

Why is my std::string obtained via stream being overwritten?

孤街浪徒 提交于 2019-12-11 13:17:19
问题 Assume I have a function like so: std::string get_shader(std::string path) { std::string fullpath = "./resources/shaders/" + path; std::ifstream vertexShaderFile(fullpath); std::ostringstream vertexBuffer; vertexBuffer << vertexShaderFile.rdbuf(); return vertexBuffer.str(); } And then some code like this: GLuint vertex_shader; GLuint fragment_shader; GLuint program; const GLchar * vertex_shader_source = get_shader("triangle_vertex.vs").c_str(); // At this point vertex_shader_source is correct

Google Protocol Buffer serialized string can contain embedded NULL characters in it?

帅比萌擦擦* 提交于 2019-12-11 12:16:52
问题 I am using Google Protocol Buffer for message serialization. This is my sample proto file content. package MessageParam; message Sample { message WordRec { optional uint64 id = 1; optional string word = 2; optional double value = 3; } message WordSequence { repeated WordRec WordSeq = 1; } } I am trying to serialize the message in C++ like following MessageParam::Sample::WordSequence wordseq; for(int i =0;i<10;i++) { AddRecords(wordseq.add_wordseq()); } std::string str = wordseq

Is there an string equivalent to LPTSTR?

强颜欢笑 提交于 2019-12-11 11:28:51
问题 Is there an string equivalent to LPTSTR? I know of string and wstring. Is there a tstring? 回答1: You could define one: typedef std::basic_string<TCHAR> mystring; ... mystring test = _T("Hello World!"); 回答2: Another option (doesn't require windows.h ): #if defined(_UNICODE) || defined(UNICODE) typedef std::wstring ustring_t; typedef wchar_t uchar_t; #define TEXT(x) (L##x) #else typedef std::string ustring_t; typedef char uchar_t; #define TEXT(x) (x) #endif Usage: ustring_t mystr = TEXT("hello

shared memory of std::string give segmentation fault (linux)

冷暖自知 提交于 2019-12-11 10:47:25
问题 I am currently trying the put structures in a shared memory between 2 process on linux. I have no problem sharing bool or int but when trying to share a string, std::string or char i have a segmentation fault error. Right now my code is : #include <iostream> #include <sys/types.h> //shmat #include <sys/shm.h> #include <sys/stat.h> //open #include <fcntl.h> #include <unistd.h> //close using namespace std; struct Prises{ int numero; int transactionId; bool reservation; bool charge; bool

avoid temporary std::string to invoke boost::unordered_map::find

ε祈祈猫儿з 提交于 2019-12-11 07:32:11
问题 I have the following type: boost::unordered_map< std::string , Domain::SomeObject > objectContainer; which is just a map to some domain object, using std::strings as keys. Now, std::string can be constructed and compared with const char* . (no need for an explicit std::string temporary, although maybe a implicit conversion is happening?) The problem happens when I try to do something like void findStuff(const char* key) { auto it = objectContainer.find(key); //<---build error } My main

Why isn't string assignment optimised when the length is known to the compiler?

戏子无情 提交于 2019-12-11 06:16:31
问题 I was playing around today with some timing code and discovered that when asigning a string literal to std::string, that it was around 10% faster (with a short 12 char string, so likly even bigger difference for large strings) to do so with a literal of known length (using the sizeof operator) than not. (Only tested with the VC9 compiler, so I guess other compilers may do it better). std::string a("Hello World!"); std::string b("Hello World!", sizeof("Hello World!");//10% faster in my tests

Lifetime of returned strings and their .c_str() [duplicate]

天涯浪子 提交于 2019-12-11 01:37:59
问题 This question already has answers here : C++: Life span of temporary arguments? (4 answers) Closed 2 years ago . I've come across multiple instance of this pattern (with boost::filesystem only used as example): boost::filesystem::path path = ...; someFunctionTakingCStrings(path.string().c_str()); where const std::string path::string() const { std::string tmp = ... return tmp; } Although I have never experienced problem with this pattern, I was wondering when the string returned by sting() is

std::string initialization with a bool

谁说我不能喝 提交于 2019-12-10 20:15:27
问题 Consider the following initialization: std::string falseString = false; std::string trueString = true; With g++ 5.2.0 , compiler throws a warning for falseString , while an error for trueString . With clang++ 3.6 -std=c++11 , compiler throws error for both falseString as well as trueString . Q1) Why the different behavior with gcc even though both initialization values are of the same type ( bool )? Q2) Which compiler is correct and why? What does the standard say? EDIT: error: no viable

How can I preallocate and initialize the character sequence inside `std::basic_string`?

梦想的初衷 提交于 2019-12-10 19:31:17
问题 I am wondering how I could preallocate and initialize the character sequence inside an ordinary C++ string. The occasion for the question is querying the Windows registry for values. See this answer for some example code. The trouble is that the system call writes into an array allocated by the caller. I've already solved the immediate problem with an allocated buffer, but this solution has what seems to be an extraneous string copy. This got me curious about the general question, one more

How to insert an integer with leading zeros into a std::string?

点点圈 提交于 2019-12-10 13:37:01
问题 In a C++14 program, I am given a string like std::string s = "MyFile####.mp4"; and an integer 0 to a few hundred. (It'll never be a thousand or more, but four digits just in case.) I want to replace the " #### " with the integer value, with leading zeros as needed to match the number of '#' characters. What is the slick C++11/14 way to modify s or produce a new string like that? Normally I would use char* strings and snprintf() , strchr() to find the " # ", but figure I should get with modern