在将其转换为 std::string 时,如何使用类型系统字符串列表中的所有元素填充向量?

How would I populate a vector with all the elements from a List of type system string while converting it to std::string?

我正在尝试更好地理解 lambda 函数,并且想要一些示例,说明如何使用这样的 Lambda 示例将 System.String^ 转换为 std::string 时添加到向量中到).

我当前的 foreach:

List<String^>^ names = //Returning 'System.String' List from C#

    for each (System::String^ name in names)
    {
      std::string convertedString = msclr::interop::marshal_as< std::string >(name);
      nameObjects.push_back(MyObject(convertedString, "test"));
    }

但我想将它扩展为这样的东西(我最好的猜测,但我缺少将 "names" 的每个元素转换为单个字符串的逻辑,这是一个Lambda 会帮助我):

    std::vector<nameObjects> testObjects{ std::begin(msclr::interop::marshal_as< std::string >(names)), std::end(msclr::interop::marshal_as< std::string >(names)) };

好吧,我想出了一个方法来完成这项工作...它需要使用晦涩难懂的 cliext 类.

首先,创建一个 cliext::vector,有一个带有 IEnumerator.

的重载
cliext::vector<String^> v_names(names);

现在,您可以使用 cliext::transform()(而非 std::transform)进行 STL 样式的迭代,并使用 lambda

创建 MyObject 个实例
std::vector<MyObject> testObjects;
cliext::transform(v_names.begin(), v_names.end(), std::back_inserter(testObjects), [](String^ name)
{
    std::string convertedString = msclr::interop::marshal_as< std::string >(name);
    return MyObject(convertedString, "test");
});