Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从C++;一个C函数_C++ - Fatal编程技术网

C++ 如何从C++;一个C函数

C++ 如何从C++;一个C函数,c++,C++,可能重复: 我必须调用一个接受字符串指针数组的c函数。范例 void cFunction(char* cities[], int count) { for(int i = 0; i < count; ++i ) { printf("%s\n", cities[i]); } } 这很有效。但我的数据是动态的,即数组的大小在运行时会发生多次变化 所以我尝试了这个 std::vector<std::string> placesB(placesA,

可能重复:

我必须调用一个接受字符串指针数组的c函数。范例

void cFunction(char* cities[], int count)
 {
   for(int i = 0; i < count; ++i )
   {
    printf("%s\n", cities[i]);
   }
 }

这很有效。但我的数据是动态的,即数组的大小在运行时会发生多次变化
所以我尝试了这个

std::vector<std::string> placesB(placesA, placesA + 5);
cFunction(&placesB[0], 5); // this won't work because std::string != char*[]
std::vector placesB(placesA,placesA+5);
c函数(&placesB[0],5);//这不起作用,因为std::string!=字符*[]

尝试了这个

std::vector<char*> placesC; 
cFunction(&placesC[0], 5);
std::vector placesC;
c函数(&placesC[0],5);

我发现同时填充
placesC
很难避免内存泄漏
我正在寻找一种既高效又有效的解决方案(尽可能少的字符串复制,最好使用STL和或Boost)

您可以编写一个函数,使用
向量
填充
向量
在每个字符串上。

您可以编写一个函数,在每个字符串上使用
.c_str()
向量填充
向量。

无论如何分割,都会有一些尴尬。如果C API确实需要可修改的数组,那么这就是您需要提供的——您必须将字符串复制到。如果它不修改字符串,那么可以使用
const char*
std::vector
,其中字符串数据仍然由底层
std::string
对象拥有;您只需小心,C API不会保留对这些字符串的引用,并在修改或取消分配字符串后尝试访问它们

例如,这里有一种方法:

// Unary functor which calls c_str() on a std::string object
struct StdStringCStrFunctor
{
    const char *operator() (const std::string& str) { return str.c_str(); }
};
...

std::vector<std::string> places;
... // populate places

// Convert to array of C strings
std::vector<const char *> placesCStr(places.size());
std::transform(places.begin(), places.end(), placesCStr.begin(), StdStringCStrFunctor());
cFunction(const_cast<char**>(&placesCStr[0]), placesCStr.size());
//对std::string对象调用c_str()的一元函子
结构StdStringCStrFunctor
{
const char*operator()(const std::string&str){return str.c_str();}
};
...
std::矢量位置;
... // 居住地点
//转换为C字符串数组
std::vector placesCStr(places.size());
std::transform(places.begin()、places.end()、placesCStr.begin()、StdStringCStrFunctor());
c函数(const_cast(&placesCStr[0]),placesCStr.size();

无论你如何分割,都会有一些尴尬。如果C API确实需要可修改的数组,那么这就是您需要提供的——您必须将字符串复制到。如果它不修改字符串,那么可以使用
const char*
std::vector
,其中字符串数据仍然由底层
std::string
对象拥有;您只需小心,C API不会保留对这些字符串的引用,并在修改或取消分配字符串后尝试访问它们

例如,这里有一种方法:

// Unary functor which calls c_str() on a std::string object
struct StdStringCStrFunctor
{
    const char *operator() (const std::string& str) { return str.c_str(); }
};
...

std::vector<std::string> places;
... // populate places

// Convert to array of C strings
std::vector<const char *> placesCStr(places.size());
std::transform(places.begin(), places.end(), placesCStr.begin(), StdStringCStrFunctor());
cFunction(const_cast<char**>(&placesCStr[0]), placesCStr.size());
//对std::string对象调用c_str()的一元函子
结构StdStringCStrFunctor
{
const char*operator()(const std::string&str){return str.c_str();}
};
...
std::矢量位置;
... // 居住地点
//转换为C字符串数组
std::vector placesCStr(places.size());
std::transform(places.begin()、places.end()、placesCStr.begin()、StdStringCStrFunctor());
c函数(const_cast(&placesCStr[0]),placesCStr.size();

然后您可以将
&charvec[0]
传递给
cFunction()
。这需要
常量转换
,但在被调用方的站点上要小心一点。使用
&字符串[0]
不需要
常量转换
(假设向量的非常量视图),但是需要C++0x,因为C++03不能保证
std::basic_string
的元素在内存中是连续的。然后您可以将
&charvec[0]
传递给
cffunction()
。这需要
const_cast
,但在被调用方的站点上要小心一点。使用
&u string[0]
不需要
const_cast
(假设向量为非const视图),但需要C++0x,因为C++03不能保证
std::basic_string
的元素在内存中是连续的。@Bertrand Marron不是重复的。我的源数据不必是std::string。我使用std::vector作为solution@Bertrand马伦不是复制品。我的源数据不必是std::string。我使用std::vector作为解决方案中的一个可能的中间步骤。这看起来很好。让我做一些测试,只是好奇这里是否没有内存泄漏。谁删除了里面的字符这看起来很整洁。让我做一些测试,只是好奇这里是否没有内存泄漏。谁删除placesCStr中的字符