This question already has an answer here:
这个问题在这里已有答案:
- converting an array of null terminated const char* strings to a std::vector< std::string > 4 answers
- copying c array of strings into vector of std::string 4 answers
将一个以null结尾的const char *字符串数组转换为std :: vector
将c数组的字符串复制到std :: string 4答案的向量中
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 char **转换为C ++向量?是否有一些内置功能可以用来做到这一点,或者通过一系列迭代步骤完成它是否更好?
EDIT: 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?
编辑:由于各种原因,C数组中的元素数量未知。我可以将其作为另一个参数传递,但这是绝对必要的吗?
4 个解决方案
#1
17
You can simply use the constructor of std::vector
that takes two iterators:
你可以简单地使用带有两个迭代器的std :: vector的构造函数:
const char* arr[] = {"Hello", "Friend", "Monkey", "Face"};
std::vector<std::string> v(std::begin(arr), std::end(arr));
Or if you really have a const char**
:
或者,如果你真的有一个const char **:
const char** p = arr;
std::vector<std::string> v(p, p + 4);
Which will also work with directly using arr
instead of p
due to array-to-pointer conversion.
由于数组到指针的转换,这也可以直接使用arr而不是p。
#2
8
char** c;
vector<string> v(c, c + 10);
Will construct elements from element of given range. 10 is number of elements here
将从给定范围的元素构造元素。 10是这里的元素数量
#3
1
You can use the constructor of std::vector
that takes two iterators, a.k.a. the range constructor:
你可以使用带有两个迭代器的std :: vector的构造函数,a.k.a。范围构造函数:
char* strings[] = {"aaa", "bbb", "ccc", "ddd"};
std::vector<std::string> v(strings, strings + 4);
where 4
is the size of your array. In this concrete example, the calculation of the size of the strings
array would be also possible by using sizeof
operator:
其中4是数组的大小。在这个具体的例子中,使用sizeof运算符也可以计算字符串数组的大小:
int len = sizeof(strings)/sizeof(char*);
std::vector<std::string> v2(strings, strings + len);
which wouldn't be possible with pure char**
though since the size of the array can not be directly retrieved from a pointer in any way (also worth to read something about array decaying).
虽然因为无法以任何方式直接从指针中检索数组的大小(这也值得阅读有关数组衰减的内容),因此使用纯char **是不可能的。
#4
0
This one-liner is useful for capturing command line arguments...
这个单行程序对捕获命令行参数很有用......
int
main(int argc, char ** argv)
{
std::vector<std::string> arguments(argv, argv + argc);
}
#1
17
You can simply use the constructor of std::vector
that takes two iterators:
你可以简单地使用带有两个迭代器的std :: vector的构造函数:
const char* arr[] = {"Hello", "Friend", "Monkey", "Face"};
std::vector<std::string> v(std::begin(arr), std::end(arr));
Or if you really have a const char**
:
或者,如果你真的有一个const char **:
const char** p = arr;
std::vector<std::string> v(p, p + 4);
Which will also work with directly using arr
instead of p
due to array-to-pointer conversion.
由于数组到指针的转换,这也可以直接使用arr而不是p。
#2
8
char** c;
vector<string> v(c, c + 10);
Will construct elements from element of given range. 10 is number of elements here
将从给定范围的元素构造元素。 10是这里的元素数量
#3
1
You can use the constructor of std::vector
that takes two iterators, a.k.a. the range constructor:
你可以使用带有两个迭代器的std :: vector的构造函数,a.k.a。范围构造函数:
char* strings[] = {"aaa", "bbb", "ccc", "ddd"};
std::vector<std::string> v(strings, strings + 4);
where 4
is the size of your array. In this concrete example, the calculation of the size of the strings
array would be also possible by using sizeof
operator:
其中4是数组的大小。在这个具体的例子中,使用sizeof运算符也可以计算字符串数组的大小:
int len = sizeof(strings)/sizeof(char*);
std::vector<std::string> v2(strings, strings + len);
which wouldn't be possible with pure char**
though since the size of the array can not be directly retrieved from a pointer in any way (also worth to read something about array decaying).
虽然因为无法以任何方式直接从指针中检索数组的大小(这也值得阅读有关数组衰减的内容),因此使用纯char **是不可能的。
#4
0
This one-liner is useful for capturing command line arguments...
这个单行程序对捕获命令行参数很有用......
int
main(int argc, char ** argv)
{
std::vector<std::string> arguments(argv, argv + argc);
}