将未知长度的char **(c)转换为vector< string> (C ++) [英] Convert char** (c) of unknown length to vector<string> (c++)

查看:119
本文介绍了将未知长度的char **(c)转换为vector< string> (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将C char **转换为C ++向量?是否可以使用某些内置功能来执行此操作,还是通过一系列迭代步骤来实现此功能更好?

How would one go about converting a C char** to a C++ vector? Is there some built-in functionality one can utilize to do this, or is it better to accomplish it through a series of iterative steps?

由于各种原因,C数组中的元素数量未知.我可以将其作为另一个参数传递,但这绝对必要吗?

For various reasons, the number of elements in the C array is unknown. It is possible I could pass that as another parameter, but is that absolutely necessary?

推荐答案

您可以简单地使用std::vector的构造函数,该构造函数需要两个迭代器:

You can simply use the constructor of std::vector that takes two iterators:

const char* arr[] = {"Hello", "Friend", "Monkey", "Face"};
std::vector<std::string> v(std::begin(arr), std::end(arr));

或者如果您确实有const char**:

const char** p = arr;
std::vector<std::string> v(p, p + 4);

由于数组到指针的转换,这也将直接使用arr而不是p.

Which will also work with directly using arr instead of p due to array-to-pointer conversion.

这篇关于将未知长度的char **(c)转换为vector&lt; string&gt; (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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