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++:

  1. Declare the strings: First, declare the strings that you want to concatenate. For example, you can declare two strings str1 and str2.
std::string str1 = "Hello";
std::string str2 = " World";
  1. Use the + operator: To concatenate strings using the + operator, simply use the + operator between the two strings.
std::string result = str1 + str2;
  1. Use the += operator: To concatenate strings using the += operator, use the += operator with the first string and the second string.
str1 += str2;
  1. Print the result: Finally, you can print the concatenated string using the cout object from the iostream 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.