string-literals

How to use memset in c++? [closed]

≯℡__Kan透↙ 提交于 2020-02-26 11:12:11
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 9 months ago . I am from Python background and recently learning C++. I was learning a C/C++ function called memset and following the online example from website https://www.geeksforgeeks.org/memset-in-cpp/ where I got some compilation errors: /** * @author : Bhishan Poudel * @file : a02_memset_geeks.cpp * @created :

Template Non-Type argument, C++11, restriction for string literals

瘦欲@ 提交于 2020-02-02 04:58:07
问题 The rules of restrictions for template non-type arguments say: A template-argument for a non-type, non-template template-parameter shall be one of: — for a non-type template-parameter of integral or enumeration type, a converted constant expression (5.19) of the type of the template-parameter; or — the name of a non-type template-parameter; or — a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage or a function

Should I compare a std::string to “string” or “string”s?

一世执手 提交于 2020-01-30 14:15:52
问题 Consider this code snippet: bool foo(const std::string& s) { return s == "hello"; // comparing against a const char* literal } bool bar(const std::string& s) { return s == "hello"s; // comparing against a std::string literal } At first sight, it looks like comparing against a const char* needs less assembly instructions 1 , as using a string literal will lead to an in-place construction of the std::string . ( EDIT: As pointed out in the answers, I forgot about the fact that effectively s

Should I compare a std::string to “string” or “string”s?

烈酒焚心 提交于 2020-01-30 14:15:39
问题 Consider this code snippet: bool foo(const std::string& s) { return s == "hello"; // comparing against a const char* literal } bool bar(const std::string& s) { return s == "hello"s; // comparing against a std::string literal } At first sight, it looks like comparing against a const char* needs less assembly instructions 1 , as using a string literal will lead to an in-place construction of the std::string . ( EDIT: As pointed out in the answers, I forgot about the fact that effectively s

Should I compare a std::string to “string” or “string”s?

与世无争的帅哥 提交于 2020-01-30 14:15:27
问题 Consider this code snippet: bool foo(const std::string& s) { return s == "hello"; // comparing against a const char* literal } bool bar(const std::string& s) { return s == "hello"s; // comparing against a std::string literal } At first sight, it looks like comparing against a const char* needs less assembly instructions 1 , as using a string literal will lead to an in-place construction of the std::string . ( EDIT: As pointed out in the answers, I forgot about the fact that effectively s

jq not replacing json value with parameter

梦想的初衷 提交于 2020-01-30 05:21:45
问题 test.sh is not replacing test.json parameter values ($input1 and $input2). result.json has same ParameterValue "$input1/solution/$input2.result" [ { "ParameterKey": "Project", "ParameterValue": [ "$input1/solution/$input2.result" ] } ] test.sh #!/bin/bash input1="test1" input2="test2" echo $input1 echo $input2 cat test.json | jq 'map(if .ParameterKey == "Project" then . + {"ParameterValue" : "$input1/solution/$input2.result" } else . end )' > result.json 回答1: shell variables in jq scripts

How can I escape special symbols in scala string?

风流意气都作罢 提交于 2020-01-22 15:05:31
问题 Is there a general Scala utility method that converts a string to a string literal? The simple lambda function "\"" + _ + "\"" only works for strings without any special characters. For example, the string \" (length 2) should be converted to the string "\\\"" (length 6). 回答1: Have a look at Apache Common's StringEscapeUtils class (docs here). escapeJava should get the job done. Have a look at this example to see it in action (in Java). 回答2: Wrap the String with 3 quotes to represent it as is

C: try to assign string literal “abc” to an array of size 3, valgrind detects error

喜欢而已 提交于 2020-01-17 06:44:59
问题 I've been thinking of what will happen if I assign a longer string literal to a char array of smaller size. (I understand that if I use a string literal as an initializer, I would probably leave out the size and let the compiler count the number of chars, or use strlen()+1 as the size. ) I have the following code: #include <stdio.h> int main() { char a[3] = "abc"; // a[2] gives an error of initializer-string for array of chars is too long printf("%s\n", a); printf("%p\n", a); } I expect it to

Pointers To Const Char

狂风中的少年 提交于 2020-01-14 07:23:12
问题 The following code points to the first character in a char array available in read-only memory. Is that right?: const char * ptr = "String one"; Now when ptr starts to point at another memory location: ptr = "String two"; What happens to the first char array ? Is that memory location freed when the execution ends? 回答1: The standard only says that string literals have static storage duration , which means that the lifetime of the variable is until the program ends and it is initialized when

What is the difference between these two ways of creating NSStrings?

风流意气都作罢 提交于 2020-01-13 17:12:12
问题 NSString *myString = @"Hello"; NSString *myString = [NSString stringWithString:@"Hello"]; I understand that using method (1) creates a pointer to a string literal that is defined as static memory (and cannot be deallocated) and that using (2) creates an NSString object that will be autoreleased. Is using method (1) bad? What are the major differences? Is there any instances where you would want to use (1)? Is there a performance difference? P.S. I have searched extensively on Stack Overflow