= ■ C++標準 / std::string ==
* std::string * 実体は「typedef basic_string<char> string;」 * 「using namespace std;」を定義しておくと便利 * std::wstring :ワイド文字(1文字表現するのに2バイト用いる文字)に対応したワイド文字列を扱う ※ ワイド文字自体が扱いづらいので基本的にstringのほうを使用 ※ string の "s" は小文字
メソッド
* 以下を参照のことhttp://www.cppll.jp/cppreference/cppstring.html
■ VC++(C++/CLI)の文字列
* System::String^
※ string の "S" は大文字なので、注意が必要。
C++標準の関数で引数が文字列でもStringは使えない。
参考文献
http://codezine.jp/article/detail/4774■ 相互変換 std::string <-> 数値
* テンプレートを使用して汎用的にした
サンプル
#include "stdafx.h"
using namespace std;
template<typename T>
T ToNumber(std::string& input)
{
std::istringstream stringstreamin(input);
T returnValue;
stringstreamin >> returnValue;
return returnValue;
}
template<class T>
std::string ToString(T input)
{
std::ostringstream stringstreamout;
stringstreamout << input;
return stringstreamout.str();
}
int main(array<System::String ^> ^args)
{
// string -> int
string numberString = "123";
int result1 = ToInt(numberString);
cout << result1 << endl;
// int -> string
int number = 456;
string result2 = ToString(number);
cout << result2 << endl;
return 0;
}
参考文献
http://d.hatena.ne.jp/pogin/20121001/1349069143■ std::string <-> System::String
== サンプル ===#include "stdafx.h"
using namespace std;
// String -> string
std::string ConvertStdString(System::String^ input)
{
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(input)).ToPointer();
string returnValue = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
return returnValue;
}
// string -> String
System::String^ ConvertSystemString(std::string& input)
{
return gcnew System::String(input.c_str());
}
int main(array<System::String ^> ^args)
{
// String -> string
String^ ex1 = "String -> string";
string output1 = ConvertStdString(ex1);
cout << output1 << endl;
// string -> String
string ex2 = "string => String";
String^ output2 = ConvertSystemString(ex2);
System::Console::WriteLine(output2);
return 0;
}
参考文献
http://msdn.microsoft.com/ja-jp/library/1b4az623.aspxhttp://hpcgi1.nifty.com/MADIA/Vcbbs/wwwlng.cgi?print+200601/06010037.txt