3

C ++ / CXにはStringBuilderクラスまたは同等のクラスがないようです。したがって、これにはSTLを使用することになっていると思いますか?

これは私の最初のC++/CXアプリです。ユーザーがTextBoxにテキストを入力し、Enterボタンを押すと、テキストがTextBlockの「コンソール」に追加されます。このコードは機能しますが、ベストプラクティスは何ですか?

public ref class MainPage sealed
{
public:
    MainPage();

protected:
    virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;
private:
    std::wstring _commandLine;
    std::wstring _consoleText;
    void Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
};

..。

void HelloConsole::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    this->_commandLine = this->InputLine->Text->Data();
    this->_commandLine.append(L"\n");
    this->_consoleText += this->_commandLine;
    this->InputLine->Text = "";
    this->OutputConsole->Text = ref new Platform::String(_consoleText.c_str());
}
4

1 に答える 1

5

C ++ / CXにはStringBuilderクラスまたは同等のクラスがないようです。したがって、これにはSTLを使用することになっていると思いますか?

はい。一般的に、C++コードではC++型を使用することをお勧めします。Windowsランタイムタイプ(などPlatform::String)は、ABI境界を越えて、またはコンポーネント境界を越えてデータを渡す場合など、必要な場合にのみ使用してください。

コードスニペットの文字列処理は問題ないように見えます。forミューテーションにコピーするのstd::wstringが妥当です。

于 2012-11-24T00:52:19.573 に答える