C++で2つの文字列を結合の方法
C++では、+演算子と+=演算子で、2つの文字列を結合できます。ここではそのどちらの方法で結合するかをサンプルコードで示します。
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello";
std::string str2 = " World";
// 使用+操作符
std::string result = str1 + str2;
std::cout << "Result using + operator: " << result << std::endl;
// 使用+=操作符
str1 += str2;
std::cout << "Result using += operator: " << str1 << std::endl;
return 0;
}
ネイティブな日本語で言い換えます。1つの選択肢のみ必要です。出力:
Result using + operator: Hello World
Result using += operator: Hello World
上記の例では、まずstr1とstr2という2つの文字列を定義し、次に+演算子を使用してそれらを結合し、その結果をresult文字列に代入します。 次に、+=演算子を使用してstr2をstr1の後尾に結合します。 最後に、+と+=演算子で得られた結果の結合文字列を出力します。