文字列の紹介
メニューを表示するにはスワイプしてください
string型を使用して、変数にテキストを格納可能。これを行うには、stringライブラリのインクルード、std名前空間のスコープ解決、string型の変数宣言が必要。これにより、プログラム内で文字の並びをシームレスに扱うことが可能。
main.cpp
1234567891011#include <iostream> #include <string> int main() { // Declaring string variable std::string text = "codefinity"; // Displaying string variable std::cout << text << std::endl; }
文字列変数には、数値(テキストとして)も格納可能。ただし、この形式で数値を格納しても、文字列のままでは直接的な数値演算はできない点に注意が必要。
main.cpp
12345678910#include <iostream> #include <string> int main() { std::string text = "1024"; // Displaying string variable std::cout << text << std::endl;; }
2つの文字列変数を加算しようとすると、連結が行われます(スペースは挿入されません)。同じことが数値にも当てはまり、代数的な加算は行われません。
main.cpp
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; }
すべて明確でしたか?
フィードバックありがとうございます!
セクション 3. 章 4
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 3. 章 4