C ++ .NET转换系统::字符串到std :: string的 [英] C++ .NET convert System::String to std::string

查看:174
本文介绍了C ++ .NET转换系统::字符串到std :: string的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么转换系统::字符串到std :: string的在C ++ .NET?

How do you convert System::String to std::string in C++ .NET?

推荐答案

有更清晰的语法,如果你使用的是最新版本的.NET

There is cleaner syntax if you're using a recent version of .net

#include "stdafx.h"
#include <string>

#include <msclr\marshal_cppstd.h>

using namespace System;

int main(array<System::String ^> ^args)
{
    System::String^ managedString = "test";

    msclr::interop::marshal_context context;
    std::string standardString = context.marshal_as<std::string>(managedString);

    return 0;
}

这也为您提供了更好的清理工作异常的脸。

This also gives you better clean-up in the face of exceptions.

有一个 MSDN文章各种其他转换

这篇关于C ++ .NET转换系统::字符串到std :: string的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆