stdstring

Difference between string += s1 and string = string + s1 [closed]

喜夏-厌秋 提交于 2019-12-03 01:11:07
One of my programs is exceeding the time limit when I am using fans = fans + s[i] , while when I am using fans += s[i] it is being accepted... Why does this happen? To Explain more , fans is a string and s is also a string so while iterating over string s i want only some characters of s so i am creating a new string fans.Now there are two ways in which i can add character to my new string fans. The Problem is mentioned below fans = fans + s[i]; // gives Time limit exceeded fans += s[i]; // runs successfully For built-in types a += b is exactly the same as a = a + b , but for classes, those

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

倾然丶 夕夏残阳落幕 提交于 2019-12-02 23:34:41
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 resources allow) Linux. I'll be using wxWidgets as my GUI layer, but I want the code that interacts with

How to efficiently get a `string_view` for a substring of `std::string`

爱⌒轻易说出口 提交于 2019-12-02 17:23:11
Using http://en.cppreference.com/w/cpp/string/basic_string_view as a reference, I see no way to do this more elegantly: std::string s = "hello world!"; std::string_view v = s; v = v.substr(6, 5); // "world" Worse, the naive approach is a pitfall and leaves v a dangling reference to a temporary: std::string s = "hello world!"; std::string_view v(s.substr(6, 5)); // OOPS! I seem to remember something like there might be an addition to the standard library to return a substring as a view: auto v(s.substr_view(6, 5)); I can think of the following workarounds: std::string_view(s).substr(6, 5); std:

std::cout doen't like std::endl and string in conditional-if

扶醉桌前 提交于 2019-12-02 11:01:25
main.cpp: In function ‘void PrintVector(std::vector<std::__cxx11::basic_string<char> >&, bool)’: main.cpp:16:41: error: overloaded function with no contextual type information std::cout << ((newline)? (std::endl) : ""); ^~ Why std::cout doen't like std::endl and string in conditional-if? std::endl is a stream manipulator. It's a function. It does not have a common type with "" . So they cannot be the two types of a conditional expression. Since the common type is the type of the whole expression. You probably don't even need everything std::endl does besides adding a new line, so just replace

if(str1==str2) versus if(str1.length()==str2.length() && str1==str2)

北慕城南 提交于 2019-12-02 01:59:22
问题 I've seen second one in another's code and I suppose this length comparison have been done to increase code productivity. It was used in a parser for a script language with a specific dictionary: words are 4 to 24 letters long with the average of 7-8 lettets, alphabet includes 26 latin letters plus "@","$" and "_". Length comparison were used to escape == operator working with STL strings, which obviously takes more time then simple integer comparison. But in the same time first letter

if(str1==str2) versus if(str1.length()==str2.length() && str1==str2)

老子叫甜甜 提交于 2019-12-02 01:16:39
I've seen second one in another's code and I suppose this length comparison have been done to increase code productivity. It was used in a parser for a script language with a specific dictionary: words are 4 to 24 letters long with the average of 7-8 lettets, alphabet includes 26 latin letters plus "@","$" and "_". Length comparison were used to escape == operator working with STL strings, which obviously takes more time then simple integer comparison. But in the same time first letter distribution in the given dictionary is simply wider than a distribution of words size, so two first letters

C++: How can i create a function that accepts concatenated strings as parameter?

对着背影说爱祢 提交于 2019-12-01 17:21:31
Can i design my logging-function in a way, that it accepts concatenated strings of the following form using C++? int i = 1; customLoggFunction("My Integer i = " << i << "."); . customLoggFunction( [...] ){ [...] std::cout << "Debug Message: " << myLoggMessage << std::endl << std::endl } Edit: Using std::string as the attribute to the function works for the concatenated string, but then a passed non-concatenated string like customLoggFunction("example string") produces a compile-time error saying the function is not applicable for char[]. When i overload the function in the following way...

C++: How can i create a function that accepts concatenated strings as parameter?

旧街凉风 提交于 2019-12-01 16:22:34
问题 Can i design my logging-function in a way, that it accepts concatenated strings of the following form using C++? int i = 1; customLoggFunction("My Integer i = " << i << "."); . customLoggFunction( [...] ){ [...] std::cout << "Debug Message: " << myLoggMessage << std::endl << std::endl } Edit: Using std::string as the attribute to the function works for the concatenated string, but then a passed non-concatenated string like customLoggFunction("example string") produces a compile-time error

std::string::assign vs std::string::operator=

孤者浪人 提交于 2019-12-01 15:56:48
I coded in Borland C++ ages ago, and now I'm trying to understand the "new"(to me) C+11 (I know, we're in 2015, there's a c+14 ... but I'm working on an C++11 project) Now I have several ways to assign a value to a string. #include <iostream> #include <string> int main () { std::string test1; std::string test2; test1 = "Hello World"; test2.assign("Hello again"); std::cout << test1 << std::endl << test2; return 0; } They both work. I learned from http://www.cplusplus.com/reference/string/string/assign/ that there are another ways to use assign . But for simple string assignment, which one is

Can std::string be used without #include <string>? [duplicate]

吃可爱长大的小学妹 提交于 2019-12-01 15:20:43
This question already has an answer here: Why does omission of “#include <string>” only sometimes cause compilation failures? 6 answers Here is my code: #include <iostream> int main(int argc, char const *argv[]) { std::string s = "hello"; std::cout << s.size() << std::endl; return 0; } To my surprise, I can compile and run it with clang++ , though I even don't add #include <string> . So, is it necessary to add #include <string> in order to use std::string ? Your implementation's iostream header includes string . This is not something which you can or should rely on. If you want to use std: