Come concatenare stringhe in c++
To concatenate strings in C++, you can use the overloaded +
operator or the +=
operator. Here are the steps to concatenate strings in C++:
- Declare the strings: First, declare the strings that you want to concatenate. For example, you can declare two strings
str1
andstr2
.
std::string str1 = "Hello";
std::string str2 = " World";
- Use the
+
operator: To concatenate strings using the+
operator, simply use the+
operator between the two strings.
std::string result = str1 + str2;
- Use the
+=
operator: To concatenate strings using the+=
operator, use the+=
operator with the first string and the second string.
str1 += str2;
- Print the result: Finally, you can print the concatenated string using the
cout
object from theiostream
library.
std::cout << result << std::endl;
Here's the complete code snippet:
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello";
std::string str2 = " World";
std::string result = str1 + str2;
std::cout << result << std::endl;
return 0;
}
This will output:
Hello World
These are the steps to concatenate strings in C++ using the +
operator and the +=
operator. Let me know if you have any further questions.