This question already has answers here:
What is the difference between a sequence point and operator precedence?
(3 answers)
Operator Precedence vs Order of Evaluation
(6 answers)
Closed last month.
I'm new to C++ and was learning about string concepts and was just experimenting around.
I have the following code:
int main(){
string s1="loc";
string s2="al var";
cout<<s1.append(s2)<<endl<<s1<<endl<<s1+s2;
}
Running it in Vs-code I get the output as:
local var
local var
local var
What I don't understand is I append s1 with s2 and it prints as "local var".
Then I print s1 and get the output "local var" meaning s1 is updated successfully.
But when I print s1+s2 I don't know why it still gives the same output as s1 and s2 isn't being concatenated with s1 in the output.
I was expecting answer as "local varal var.
I want to know about how and why is this happening.
Thanks!
Related
This question already has answers here:
escape R"()" in a raw string in C++
(2 answers)
Include )" in raw string literal without terminating said literal
(3 answers)
Closed 9 months ago.
I want to output a string like this: onclick="func()". So I wrote the following code:
std::string s = R"(
onclick="func()"
)";
But here two )" let the compiler confused.
Please forgive me if it's a silly question.
I googled but found nothing (I don't know which keyword I should use when I googled).
Simply add a unique string outside the ()
std::string s = R"anystring(
onclick="func()"
)anystring";
This question already has answers here:
Why operator `?:` does not have priority?
(7 answers)
Closed 1 year ago.
I was writing code using ternary operator instead of if/else to save some time in Competitive Programming. But I was stuck in one case and here is the code
std::cout<< a%2? "string1" : "string2";
OUTPUT on my system: 0 or 1 as per 'a'
Excepted: string1 or string2
I think here ternary operator is returning "const char *" after evaluation, but I'm not getting expected results. So here are my doubts about it.
Why const char * ram = "ram"; std::cout<<ram; works fine but above code don't, to me both seems to have const char * as input to cout, so what is difference between them?
Any workaround to print strings as intended above using ternary operator?
operator<< has higher precedence than ternary conditional operator, so std::cout<< a%2? "string1" : "string2"; has the same effect as (std::cout<< a%2) ? "string1" : "string2";. As the result a%2 is printed out instead. (std::cout<< a%2 returns std::cout, which could convert to bool, regardless of the result is true or false, "string1" or "string2" don't have any effect here.)
You should add parentheses like
std::cout<< (a%2? "string1" : "string2");
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 6 years ago.
Even though the name of the string signifies address of the first element in the string,when I perform this:
char str[]="dog";
cout<<str<<endl;
How come the whole string gets printed?
In C++, the "<<" operator is "smart". It knows the types of the thing to its left and to its right, and if the thing to its right is a character pointer and the thing to its left is an output stream, it prints the string rather than the value of the pointer.
In C, you have to tell printf() which one to do with "%p" or "%s", but "<<" makes that choice for you.
Google "C++ operator overloading"
This question already has answers here:
What is the type of a string literal in C++? [duplicate]
(2 answers)
Closed 7 years ago.
When I use :
std::cout << "Hello world ";
Which type is "Hello world" ?
Where does it stored , so I can get it out and work with it ?
For some reasons, I don't want to use something like :
std::string str = "Hello world";
std::cout << str;
Please help me, I searched an hour but still no answer.
The type of a string literal is "constant array of char", with as many elements as characters in the literal, plus one for a final null character. Other versions of string literals (wide, unicode) are arrays of other character types (wchar_t, char16_t etc.) (e.g. see here).
This question already has answers here:
Get the last element of a std::string
(4 answers)
Closed 7 years ago.
In python you can say print "String"[-1] and it would print be the last character, 'g'. Is there an equivalent for this in c++?
You can use string.back() to get a reference to the last character in the string. The last character of the string is the first character in the reversed string, so string.rbegin() will give you an iterator to the last character.
Use the back() function for std::string:
std::string str ("Some string");
cout << str.back()
Output:
g
For C strings, it is
String[strlen(String) - 1];
For C++ style strings, it is either
String.back();
*String.rbegin();
String[String.length() - 1];
You can use the function:
my_string.back();
If you want to output it, then:
#include <iostream>
std::cout << my_string.back();