How to Concatenate a Std::String and an Int

string and int concatenation in C++ [duplicate]

C++ is not Java.

In C++, "word" + i is pointer arithmetic, it's not string concatenation. Note that the type of string literal "word" is const char[5] (including the null character '\0'), then decay to const char* here. So for "word" + 0 you'll get a pointer of type const char* pointing to the 1st char (i.e. w), for "word" + 1 you'll get pointer pointing to the 2nd char (i.e. o), and so on.

You could use operator+ with std::string, and std::to_string (since C++11) here.

words[i] = "word" + std::to_string(i);

BTW: If you want word1 ~ word5, you should use std::to_string(i + 1) instead of std::to_string(i).

How to concatenate a string and an integer

You can use std::to_string to create std::string from your int values

string s1 = std::to_string(h) + ":" + std::to_string(m) + ":" + std::to_string(s);

Remember you have to return from your function!

string whatTime(int n)
{
int h = n / 3600;
int m = n / 60;
int s = n % 60;

return to_string(h) + ":" + to_string(m) + ":" + to_string(s);
}

Concatenation of std::string and int leads to a shift. Why?

Does the string for some reason get converted into char[]

Actually it is the other way around. "iteration_" is a char[11] which decays to a const char* when you add 1. Incrementing the pointer by one makes it point to the next character in the string. This is then used to construct a temporary std::string that contains all but the first character.

The documentation you link is for operator+ of std::string, but to use that you need a std::string first.

Append int to std::string

TL;DR operator+= is a class member function in class string, while operator+ is a template function.

The standard class template<typename CharT> basic_string<CharT> has overloaded function basic_string& operator+=(CharT), and string is just basic_string<char>.

As values that fits in a lower type can be automatically cast into that type, in expression s += 2, the 2 is not treated as int, but char instead. It has exactly the same effect as s += '\x02'. A char with ASCII code 2 (STX) is appended, not the character '2' (with ASCII value 50, or 0x32).

However, string does not have an overloaded member function like string operator+(int), s + 2 is not a valid expression, thus throws an error during compilation. (More below)

You can use operator+ function in string in these ways:

s = s + char(2); // or (char)2
s = s + std::string(2);
s = s + std::to_string(2); // C++11 and above only

For people concerned about why 2 isn't automatically cast to char with operator+,

template <typename CharT>
basic_string<CharT>
operator+(const basic_string<CharT>& lhs, CharT rhs);

The above is the prototype[note] for the plus operator in s + 2, and because it's a template function, it is requiring an implementation of both operator+<char> and operator+<int>, which is conflicting. For details, see Why isn't automatic downcasting applied to template functions?

Meanwhile, the prototype of operator+= is:

template <typename CharT>
class basic_string{
basic_string&
operator+=(CharT _c);
};

You see, no template here (it's a class member function), so the compiler deduces that type CharT is char from class implementation, and int(2) is automatically cast into char(2).


Note: Unnecessary code is stripped when copying from C++ standard include source. That includes typename 2 and 3 (Traits and Allocator) for template class "basic_string", and unnecessary underscores, in order to improve readability.

How would you concatenate on the fly a text+integer passing it to a function? [duplicate]

In C++ "Text" is a const char[N], it's not actually a string type but just an array of characters with a null character ('\0') at the end. This doesn't support any sort of string manipulation. What you need to get is a std::string, which does support many string operations. Since you need to convert mIndex to a string to begin with we can just to that and the string that represents the number will handle concatenating "Text" to it. That gives you

int mIndex = 0;
InitBool(("Text" + std::to_string(mIndex)).c_str());

The ("Text" + std::to_string(mIndex)) part gives you a temporary std::string that is "Text0" and then the .c_str() gets a const char* to that string to pass to the function.


You can wrap the ("Text" + std::to_string(mIndex)) part in a function like

std::string concat(const char* str, int val)
{
return str + std::to_string(val);
}

and then the function call would look like

InitBool(concat("Text", mIndex).c_str());

Very basic question on integer variable and string concatenation using + in C++ [duplicate]

The reason you get some weird output is a part of C++'s history being an evolution of C. In the expression x + " is greater than " + y the expression " is greater than " is a const char* literal which is a raw C-style type. It is not a C++ class type like std::string due to backwards compatibility reasons.

Your + signs add integers to a const char*. This results in pointer arithmetic. Basically, " is greater than " being a const char*, it is a pointer to some buffer of memory whose contents are ASCII bytes for " is greater than ". The effect of adding x and y to that is to move the pointer to the right 35 bytes where it will go off the end of the buffer and read uninitialized memory. That is the "odd characters" that come out. Properly speaking this is undefined behavior so anything could happen. In real systems though this will just be a buffer overflow read resulting in gibberish characters.

As others have noted, a way to fix this would be to use std::to_string on the integers. Then, instead of int + const char* + int you would get std::string + const char* + std::string which is handled much better.

If this is unclear you can look up pointers, C-style strings, and buffer overflows for more info.

Edit: Technically speaking string literals are const char[] but I have omitted this for clarity.

How do you concatenate strings and integers in C++? [duplicate]

In C++03, as other people have mentioned, you can use the ostringstream type, defined in <sstream>:

std::ostringstream stream;
stream << "Mixed data, like this int: " << 137;
std::string result = stream.str();

In C++11, you can use the std::to_string function, which is conveniently declared in <string>:

std::string result = "Adding things is this much fun: " + std::to_string(137);

Hope this helps!

How do I concatenate multiple C++ strings on one line?

#include <sstream>
#include <string>

std::stringstream ss;
ss << "Hello, world, " << myInt << niceToSeeYouString;
std::string s = ss.str();

Take a look at this Guru Of The Week article from Herb Sutter: The String Formatters of Manor Farm



Related Topics



Leave a reply



Submit