Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 文字列のイントロダクション | 変数とデータ型
C++入門

book文字列のイントロダクション

メニューを表示するにはスワイプしてください

string型を使用して、変数にテキストを保存可能。これを行うには、stringライブラリのインクルード、std名前空間のスコープ解決、string型の変数宣言が必要。これにより、プログラム内で文字の並びをシームレスに扱うことが可能。

main.cpp

main.cpp

copy
1234567891011
#include <iostream> #include <string> int main() { // Declaring string variable std::string text = "codefinity"; // Displaying string variable std::cout << text << std::endl; }

文字列変数には、数値(テキストとして)も含めることが可能。ただし、この形式で数値を保存しても、文字列として保存されている間は直接的な数値演算はできない点に注意が必要。

main.cpp

main.cpp

copy
12345678910
#include <iostream> #include <string> int main() { std::string text = "1024"; // Displaying string variable std::cout << text << std::endl;; }

2つの文字列変数を加算しようとすると、連結が行われます(スペースは挿入されません)。同じことが数値にも当てはまり、代数的な加算にはなりません

main.cpp

main.cpp

copy
1234567891011
#include <iostream> #include <string> int main() { std::string first_part = "Hello "; //space is also a symbol std::string second_part = "World"; //displaying the sum of string variables std::cout << first_part + second_part << std::endl; }
question mark

次のコードの問題点は何ですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  4

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  4
some-alt