int型、実数のdouble型など、すべての値に型があります。int型の変数、などということがあります。#include <ファイル名>で読み込みます。std::wstring型を使います。
#include <Windows.h>
//std::wstringを定義しているライブラリ
#include <string>
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int) {
//宣言、初期化
std::wstring text = L"Welcome back!";
//メッセージボックスに渡す
MessageBoxW(NULL, text.c_str(), L"MessageBoxW1", MB_OK);
return 0;
}
型名 変数名という構文を使います。std::string型の変数textを宣言しました。
= L"Welcome back"とついています。std::string型の初期化をしなかった場合、空の文字列が初期値になります。
text.c_str()の部分で値を利用しています。c_str関数で変換しています。_です。
#include <Windows.h>
#include <string>
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int) {
std::wstring text = L"First";
MessageBoxW(NULL, text.c_str(), L"MessageBoxW1", MB_OK);
//代入
text = L"Second";
MessageBoxW(NULL, text.c_str(), L"MessageBoxW2", MB_OK);
return 0;
}
text = L"Second";で、変数textに、値L"Second"を代入しています。MessageBoxWの第2引数は同じでも、違う値が表示されるのです。変数名 = 値です。
#include <Windows.h>
#include <string>
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int) {
std::wstring text = L"Hello, ";
//連結と代入
text = text + L"world";
MessageBoxW(NULL, text.c_str(), L"MessageBoxW1", MB_OK);
return 0;
}
text + L"world"で、変数textの値に、L"world"を連結しています。textに代入しています。+の左側の値が前になります。
MessageBoxWで、詰めた後箱の中に入っている果物を列挙してください。
#include <Windows.h>
#include <string>
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int) {
std::wstring str0 = L"リンゴ";//月曜日に加える果物
return 0;
}
#include <Windows.h>
#include <string>
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int) {
std::wstring str = L"リンゴ"; //月曜日に加える果物
MessageBoxW(NULL, str.c_str(), L"月曜日", MB_OK);//strに入っている文字列を表示
str = str + L" バナナ"; //火曜日に加える果物の文字列を連結
MessageBoxW(NULL, str.c_str(), L"火曜日", MB_OK);//以下繰り返し
str = str + L" イチゴ";
MessageBoxW(NULL, str.c_str(), L"水曜日", MB_OK);
str = str + L" 洋ナシ";
MessageBoxW(NULL, str.c_str(), L"木曜日", MB_OK);
str = str + L" ブドウ";
MessageBoxW(NULL, str.c_str(), L"金曜日", MB_OK);
str = str + L" メロン";
MessageBoxW(NULL, str.c_str(), L"土曜日", MB_OK);
str = str + L" ドリアン";
MessageBoxW(NULL, str.c_str(), L"日曜日", MB_OK);
return 0;
}