How to Append a Char to a Std::String

How to append a char to a std::string?

y += d;

I would use += operator instead of named functions.

Append multiple chars to string in C++

This has to do with operator precedence. Let's take for example the expression:

string s = "";
s += '0' + '1';//here + has higher precedence than +=

Here + has higher precedence than += so the characters '0' and '1' are group together as ('0' + '1'). So the above is equivalent to writing:

s+=('0' + '1');

Next, there is implicit conversion from char to int of both '0' and '1'. So '0' becomes the decimal 48 while the character '1' becomes decimal 49.

So essentially the above reduces to:

s+=(48 + 49);

or

s+=(97);

Now 97 corresponds to the character a and therefore the final result that is appended to the string variable named s is the character a.

Now lets apply this to your example:

string s = "";
s += 'a' + 'b';

First due to precedence:

s+=('a' + 'b');

Second implicit conversion from char to int happens:

s+=(97 + 98);

So

s+=195;

So the character corresponding to decimal 195 will be appended to string s.

You can try out adding and subtracting different characters to confirm that this is happening.

Why does the first example append the char and the second does not?

So the fundamental reason is operator precedence.

append chars to string

"(" is not a std::string. It is a char[2] C string array. Make it std::string by using the s literal:

using namespace std::string_literals;
std::string s = "("s + a + ","s + b + ")"s;

This can still fail if you try to do something like this:

std::string s = a + b + "."s; // error

In this case you can simply start with an empty string:

std::string s = ""s + a + b + "."s;

Another option is to use std::ostringstream to build the string:

std::ostringstream oss;
oss << "(" << a << "," << b << ")";
std::string s3 = oss.str();

How do I concatenate strings with chars in C++?

The function should be declared like:

void print( const std::string &myString) 
{
std::cout << myString;
}

and called like:

print( std::string( "Error, unexpected char: " ) + myChar + "\n");

As for your comment:

as a follow up, would it have been possible to pass an anonymous
function returning a string as an argument to print()? Something like
print( {return "hello world";}

then you can do this as it is shown in the demonstration program:

#include <iostream>
#include <string>

void f( std::string h() )
{
std::cout << h() << '\n';
}

int main()
{
f( []()->std::string { return "Hello World!"; } );

return 0;
}

How to append a char type variable to a string?

This should work:

ssifre += pass;

It's using the += operator

(note that as @MichaelDoubez mentionned in the comments, ssifre.append(pass) would work, too.)

c++14 - Is this a good way to prepend a char on a string?

Since both perform the same operation, what reason could there be besides efficiency? c+s will create a temporary string, thus requiring a copy of every character in both c and s, and potentially a heap allocation. The temporary will then be moved into the given object, which will have its current memory deallocated (if any). These are not cheap operations.

By contrast, insert will only perform a heap allocation if there is insufficient capacity for the new character. You'll still have the copying going on, since you're inserting at the beginning. But that's about it. It is as efficient as insertion at the head of a contiguous array gets.



Related Topics



Leave a reply



Submit