Strange behaviour of << (as at least seems to me) - c++

I can't realize how could it be possible to print a string this way without any complaint by the compiler:
std::cout << "Hello " "World!";
In fact, the above line works exactly like:
std::cout << "Hello " << "World!";
Is there an explanation for this behaviour?

Adjacent literal tokens are concatenated automatically, it's part of the standard.
2.1 Phases of translation [lex.phases]
6) Adjacent ordinary string literal tokens are concatenated. Adjacent wide string literal tokens are concatenated.
(C++03)

In C++, literals tokens can be concatenated thusly:
const char* thingy = "Hello" "World";
"Hello" and "World" are each a literal token.

This is normal behavior of the strings. In the first line specified strings are concatenated by compiler automatically. As sample you can specify also multiline to avoid very long line.
const char *strLine = "line 1 "
"line 1 "
"line 2 ";
And it will work OK. The second line is cleared, specified another line for output.

Related

Using one cout command to print multiple strings with each string placed on a different (text editor) line

Take a look at the following example:
cout << "option 1:
\n option 2:
\n option 3";
I know,it's not the best way to output a string,but the question is why does this cause an error saying that a " character is missing?There is a single string that must go to stdout but it just consists of a lot of whitespace charcters.
What about this:
string x="
string_test";
One may interpret that string as: "\nxxxxxxxxxxxxstring_test" where x is a whitespace character.
Is it a convention?
That's called multiline string literal.
You need to escape the embedded newline. Otherwise, it will not compile:
std::cout << "Hello world \
and stackoverflow";
Note: Backslashes must be immediately before the line ends as they need to escape the newline in the source.
Also you can use the fun fact "Adjacent string literals are concatenated by the compiler" for your advantage by this:
std::cout << "Hello World"
"Stack overflow";
See this for raw string literals. In C++11, we have raw string literals. They are kind of like here-text.
Syntax:
prefix(optional) R"delimiter( raw_characters )delimiter"
It allows any character sequence, except that it must not contain the
closing sequence )delimiter". It is used to avoid escaping of any
character. Anything between the delimiters becomes part of the string.
const char* s1 = R"foo(
Hello
World
)foo";
Example taken from cppreference.

Removing expressions from QString using QRegExp

I'm having an issue removing expressions from a QString using QRegExp. I tried a countless number of regex to no avail. What am I doing wrong?
Sample Text (QString myString) In this instance, myString contains "\u0006\u0007\u0013Hello".
myString.remove(QRegExp("\\[u][0-9]{4}"));
It does not remove any instances of \uXXXX where X = numbers.
However, when I am specific such as:
myString.remove("\u0006");
It does remove it.
String literals are not always the same as character sequence
for (char c : "\u0006\u0007\u0013Hello".toCharArray()) {
System.out.println( c + " (" + (int)c + ")" );
}
System.out.println( "--------------" );
for (char c : "\\u0006\\u0007\\u0013Hello".toCharArray()) {
System.out.println( c + " (" + (int)c + ")" );
}
In the first example \u0006 is encoding an unicode code point, whereas in second the string actually contains a backslash.
The string literal only exist at compile time, at runtime they are character sequences.
Regexes are working over character sequence not over string litteral, and also backlash have special meaning and need to be escaped.
Also note that \u0041 is another way to encode A.
Maybe what you are looking for are unicode categories, maybe following can help:
string.replaceAll( "\\p{Cc}", "" )

C++ , How can I ignore comma (,) from csv char *?

I have searched a lot about it on SO and solutions like "" the part where comma is are giving errors. Moreover it is using C++ :)
char *msg = new char[40];
msg = "1,2, Hello , how are you ";
char msg2[30];
strcpy_s(msg2, msg);
char * pch;
pch = strtok(msg2, ",");
while (pch != NULL)
{
cout << pch << endl;
pch = strtok(NULL, ",");
}
Output I want :
1
2
Hello , how are you
Out put it is producing
1
2
Hello
how are you
I have tried putting "" around Hello , how are you. But it did not help.
The CSV files are comma separated values. If you want a comma inside the value, you have to surround it with quotes.
Your example in CSV, as you need your output, should be:
msg = "1,2, \"Hello , how are you \"";
so the value Hello , how are you is surrounded with quotes.
This is the standard CSV. This has nothing to do with the behaviour of the strtok function.
The strtok function just searches, without considering anything else, the tokens you have passed to it, in this case the ,, thus it ignores the ".
In order to make it work as you want, you would have to tokenize with both tokens, the , and the ", and consider the previous found token in order to decide if the , found is a new value or it is inside quotes.
NOTE also that if you want to be completely conforming with the CSV specification, you should consider that the quotes may also be escaped, in order to have a quote character inside the value term. See this answer for an example:
Properly escape a double quote in CSV
NOTE 2: Just for completeness, here is the CSV specification (RFC-4180): https://www.rfc-editor.org/rfc/rfc4180

C++ multiline string raw literal

We can define a string with multiline like this:
const char* text1 = "part 1"
"part 2"
"part 3"
"part 4";
const char* text2 = "part 1\
part 2\
part 3\
part 4";
How about with raw literal, I tried all, no one works
std::string text1 = R"part 1"+
R"part 2"+
R"part 3"+
R"part 4";
std::string text2 = R"part 1"
R"part 2"
R"part 3"
R"part 4";
std::string text3 = R"part 1\
part 2\
part 3\
part 4";
std::string text4 = R"part 1
part 2
part 3
part 4";
Note that raw string literals are delimited by R"( and )" (or you can add to the delimiter by adding characters between the quote and the parens if you need additional 'uniqueness').
#include <iostream>
#include <ostream>
#include <string>
int main ()
{
// raw-string literal example with the literal made up of separate, concatenated literals
std::string s = R"(abc)"
R"( followed by not a newline: \n)"
" which is then followed by a non-raw literal that's concatenated \n with"
" an embedded non-raw newline";
std::cout << s << std::endl;
return 0;
}
Just write it as you want it:
std::string text = R"(part 1
part 2
part 3
part 4)";
The other thing you didn't put in was the required pair of parentheses around the entire string.
Also keep in mind any leading spaces on the part 2-4 lines that you might put in to keep the code formatted are included, as well as a leading newline to get part 1 with the others, so it does make it rather ugly to see in the code sometimes.
An option that might be plausible for keeping things tidy, but still using raw string literals is to concatenate newlines:
R"(part 1)" "\n"
R"(part 2)" "\n"
R"(part 3)" "\n"
R"(part 4)"

Is this regular expression?

This is how to split string in Unityscript from Unity Wiki. However, I don't recognize " "[0]. Is this regular expression? If so, any reference to it? I'm familiar with regular expressions generally and used them a lot, but this syntax is little confusing.
var qualifiedName = "System.Integer myInt";
var name = qualifiedName.Split(" "[0]);
Wiki Reference
On any string, wether it is a variable or a literal (" "), you can use an indexer to get the char at the nth position.
Your codesample is a very weird way of literally defining a char with a space, and could be simplified by using this:
' '
note the single quotes instead of double quotes
As many have already mentioned, " "[0] is the first character of the " " string (which is a System.String, or an array of System.Chars. The problem with UnityScript is that ' ' is interpreted as a String too, so the only way to provide a Char is by slicing.
" "[0] is the first character of the string " ".
typeof " "[0]; // "string"
Your example is strange, because " "[0] and " " are strictly equal.
" "[0] === " "; // true
Reading reference:
Mono Types When a Mono function requires a char as an input, you can
obtain one by simply indexing a string. E.g. if you wanted to pass the
lowercase a as a char, you'd write: "a"[0]
I suppose it's because UnityScript is implemented in Boo and String is provided by mono.