stdstring

Why must a string be constructed at run-time? [duplicate]

喜欢而已 提交于 2019-12-10 01:07:22
问题 This question already has answers here : Is it possible to use std::string in a constexpr? (4 answers) Closed 4 years ago . Can C-Strings or std::string s be created as constexpr or must they be created at run-time? With gcc 4.9.2 I can do this: constexpr const char foo[] = "blee"; (Sadly the November 2013 Customer Technology Preview does not allow Visual Studio to support this: https://stackoverflow.com/a/29255013/2642059) But even with gcc 4.9.2 I cannot do this: constexpr const std::string

Specific behaviour of std::string on visual studio?

烂漫一生 提交于 2019-12-09 17:50:25
问题 I've got a project in which I need to read/write large files. I've decided to use ifstream::read() to put those files into memory in one single pass, into an std::string. (that seems to be the fastest way to do it in c++ : http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html and http://insanecoding.blogspot.com/2011/11/reading-in-entire-file-at-once-in-c.html) When switching between files, I then need to "reset" the std::string used as the previous memory buffer (ie, erase

std::string copy constructor NOT deep in GCC 4.1.2?

限于喜欢 提交于 2019-12-09 14:31:06
问题 I wonder if i misunderstood something: does a copy constructor from std::string not copy its content? string str1 = "Hello World"; string str2(str1); if(str1.c_str() == str2.c_str()) // Same pointers! printf ("You will get into the IPC hell very soon!!"); This will print "You will get into the IPC hell very soon!!" and it annoys me. Is this the normal behavior of std::string ? I read somewhere that it usually does a deep copy. However, this works as expected: string str3(str1.c_str()); if

Compile error with templates - no matching function for call

假装没事ソ 提交于 2019-12-09 12:24:32
问题 I'm trying to convert a string to a number. For that, I found the following way: #include <iostream> #include <string> template <typename T> T stringToNumber(const std::string &s) { std::stringstream ss(s); T result; return ss >> result ? result : 0; } int main() { std::string a = "254"; int b = stringToNumber(a); std::cout << b*2 << std::endl; } The problem is that I am getting the following error: error: no matching function for call to ‘stringToNumber(std::string&)’ May anyone tell me why

Why does std::string(“\x00”) report length of 0?

五迷三道 提交于 2019-12-09 05:02:27
问题 I have a function which needs to encode strings, which needs to be able to accept 0x00 as a valid 'byte'. My program needs to check the length of the string, however if I pass in "\x00" to std::string the length() method returns 0. How can I get the actual length even if the string is a single null character? 回答1: You're passing in an empty string. Use std::string(1, '\0') instead. Or std::string{ '\0' } (thanks, @zett42) 回答2: std::string is perfectly capable of storing nulls. However, you

C++ Read From Socket into std::string

最后都变了- 提交于 2019-12-08 18:46:44
问题 I am writing a program in c++ that uses c sockets. I need a function to receive data that I would like to return a string. I know this will not work: std::string Communication::recv(int bytes) { std::string output; if (read(this->sock, output, bytes)<0) { std::cerr << "Failed to read data from socket.\n"; } return output; } Because the read() * function takes a char array pointer for an argument. What is the best way to return a string here? I know I could theoretically read the data into a

Any advantage of using the s suffix in C++ [duplicate]

假如想象 提交于 2019-12-08 16:04:41
问题 This question already has answers here : Advantages of using user-defined literal for strings instead of string literal (4 answers) Closed last year . My question is related to the use of the "s" suffix in C++? Example of code using the "s" suffix: auto hello = "Hello!"s; // a std::string The same could be written as: auto hello = std::string{"Hello!"}; I was able to find online that the "s" suffix should be used to minimizes mistakes and to clarify our intentions in the code. Therefore, is

C++ : Does char pointer to std::string conversion copy the content?

守給你的承諾、 提交于 2019-12-08 14:55:56
问题 When I convert a char* to std::string using the constructor: char *ps = "Hello"; std::string str(ps); I know that std containers tend to copy values when they are asked to store them. Is the whole string copied or the pointer only? if afterwards I do str = "Bye" will that change ps to be pointing to "Bye"? 回答1: std::string object will allocate internal buffer and will copy the string pointed to by ps there. Changes to that string will not be reflected to the ps buffer, and vice versa. It's

Does basic_string class really have copy constructor that takes more than one parameter or is it just constructor?

被刻印的时光 ゝ 提交于 2019-12-08 05:42:09
问题 I was reading Why would a copy constructor have more than one parameter?. The accepted answer says that: The old std::basic_string does have one too: basic_string(const basic_string& s, size_type pos = 0, size_type n = npos) But http://www.cplusplus.com/reference/string/basic_string/basic_string/ says that: basic_string (const basic_string& str, size_type pos, size_type len = npos, const allocator_type& alloc = allocator_type()); The above isn't a copy constructor but substring constructor

Reverse the string in C++

一曲冷凌霜 提交于 2019-12-07 18:48:22
问题 #include <iostream> #include <cstdlib> using namespace std; main() { beginning: string name; cout << "Hey! Enter your name" << endl; cin >> name; int i = name.length() - 1; do { cout << name[i]; i--; } while (i > -1); cout << " Your reverse name is " << name[i] << endl; cout << name[i] << " Your reverse name is " << endl; goto beginning; } Why the "zsuidakrA" is being displayed before "Your reverse name is" although I have coded like cout<<" Your reverse name is "<<name[i]<<endl; For cout<