string-literals

what does cout << “\n”[a==N]; do?

假如想象 提交于 2019-11-30 06:29:05
问题 In the following example: cout<<"\n"[a==N]; I have no clue about what the [] option does in cout , but it does not print a newline when the value of a is equal to N . 回答1: I have no clue about what the [] option does in cout This is actually not a cout option, what is happening is that "\n" is a string literal. A string literal has the type array of n const char , the [] is simply an index into an array of characters which in this case contains: \n\0 note \0 is appended to all string literals

Is the u8 string literal necessary in C++11

孤街醉人 提交于 2019-11-30 04:55:18
From Wikipedia : For the purpose of enhancing support for Unicode in C++ compilers, the definition of the type char has been modified to be at least the size necessary to store an eight-bit coding of UTF-8. I'm wondering what exactly this means for writing portable applications. Is there any difference between writing this const char[] str = "Test String"; or this? const char[] str = u8"Test String"; Is there be any reason not to use the latter for every string literal in your code? What happens when there are non-ASCII-Characters inside the TestString? The encoding of "Test String" is the

Swift string via string literal vs initializer

丶灬走出姿态 提交于 2019-11-30 03:38:34
问题 In other languages such as Java, under the hood there is actually a difference between string obtained via string literal vs initializer. In Swift, are they equivalent under the hood? e.g. var string:String = "" var string:String = String() Refer to this SO post for info on differences between literal and object in Java. 回答1: The declarations are equivalent according to the Apple docs: Initializing an Empty String To create an empty String value as the starting point for building a longer

C standard : Character set and string encoding specification

牧云@^-^@ 提交于 2019-11-30 02:44:41
问题 I found the C standard (C99 and C11) vague with respect to character/string code positions and encoding rules: Firstly the standard defines the source character set and the execution character set . Essentially it provides a set of glyphs, but does not associate any numerical values with them - So what is the default character set? I'm not asking about encoding here but just the glyph/repertoire to numeric/code point mapping. It does define universal character names as ISO/IEC 10646, but does

Inconsistency between std::string and string literals

雨燕双飞 提交于 2019-11-30 02:44:24
I have discovered a disturbing inconsistency between std::string and string literals in C++0x: #include <iostream> #include <string> int main() { int i = 0; for (auto e : "hello") ++i; std::cout << "Number of elements: " << i << '\n'; i = 0; for (auto e : std::string("hello")) ++i; std::cout << "Number of elements: " << i << '\n'; return 0; } The output is: Number of elements: 6 Number of elements: 5 I understand the mechanics of why this is happening: the string literal is really an array of characters that includes the null character, and when the range-based for loop calls std::end() on the

How to write unicode cross symbol in Java?

白昼怎懂夜的黑 提交于 2019-11-30 00:43:21
I'm trying to write this unicode cross symbol ( 𐀵 ) in Java: class A { public static void main(String[] args) { System.out.println("\u2300"); System.out.println("\u10035"); } } I can write o with a line through it ( ⌀ ) just fine, but the cross symbol doesn't show up, instead it just prints the number 5: # javac A.java && java A ⌀ ဃ5 Why? You're looking for U+10035, which is outside the Basic Multilingual Plane . That means you can't use \u to specify the value, as that only deals with U+0000 to U+FFFF - there are always exactly four hex digits after \u . So currently you've got U+1003 (

Some const char * are unavailable at compile time?

限于喜欢 提交于 2019-11-30 00:37:59
问题 Let's suppose we have a template function with non-type parameter of const char * like this: template <const char * MESSAGE> void print() { std::cout << MESSAGE << '\n'; } Using this template wouldn't be a problem as log as the MESSAGE can be deduced at compile-time, so the following uses are legal: namespace { char namespace_message[] = "Anonymous Namespace Message"; constexpr char namespace_constexpr_message[] = "Anonymous Namespace Constexpr Message"; } char message[] = "Message";

Something like print END << END; in C++?

跟風遠走 提交于 2019-11-29 17:49:18
问题 Is there anyway to do something like PHP's print << END yadayadayada END; in C++? (multi-line, unescaped, easy-to-cut-and-paste stream insertion) 回答1: This answer is now out of date for modern C++ - see sbi's answer for the modern way. This is the best you can do: std::cout << "This is a\n" "multiline\n" "string.\n"; Not as convenient as a proper heredoc, but not terrible. 回答2: C++11 has raw string literals: // this doesn't have '\n', but '\\' and 'n' R"(yada"yadayada\n)" And if you need

shared c constants in a header

╄→尐↘猪︶ㄣ 提交于 2019-11-29 16:08:03
问题 I want to share certain C string constants across multiple c files. The constants span multiple lines for readability: const char *QUERY = "SELECT a,b,c " "FROM table..."; Doing above gives redefinition error for QUERY. I don't want to use macro as backspace '\' will be required after every line. I could define these in separate c file and extern the variables in h file but I feel lazy to do that. Is there any other way to achieve this in C? 回答1: In some .c file, write what you've written. In

What does assigning a literal string to an NSString with “=” actually do?

三世轮回 提交于 2019-11-29 13:32:45
What does the following line actually do? string = @"Some text"; Assuming that "string" is declared thusly in the header: NSString *string; What does the "=" actually do here? What does it do to "string"'s reference count? In particular, assuming that for some reason "string" is not otherwise assigned to, does it need to be released? Thanks! The assignment is just that. The string pointer is basically a label that points to specific address in memory. Reassignment statement would point that label to another address in memory! It doesn't change reference counting or do anything beyond that in