convert vector< string> into char ** C ++ [英] convert vector<string> into char** C++

查看:121
本文介绍了convert vector< string> into char ** C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量< std :: string> 变量。我需要将它传递给接受 char ** 作为输入参数的方法。



如何做?如果可能,我需要通过一个可写的。



更新1:
在用于创建服务方法的工具中,我将参数设置为std :: vector,自动将限定符作为&,这意味着我的工具生成的方法定义将如下所示:

  std :: string SvcImpl: :myMethodname(const std :: string par1,const std :: vector< std :: string>& par2,const std :: vector< std :: string>& par3)
{

}

此方法将自动调用传递的参数中的值。
现在从这个方法内部我将调用lib文件夹中的dll中的方法,如下所示:

  int method_to_be_called(char * par1,char ** par2,char ** par3,void * pRetValue); 

对于par1 - >我传递(char *)par1.c_str >

我需要知道如何传递par2和par3以及pRetValue的变量。
par2和par3的值在向量中可用,但最后一个参数pRetValue是一个输出参数,我需要将其作为std :: string返回。



对不起如果我很混乱或提出非常基本的问题。

解决方案

有可能解决这个问题而不复制所有 std :: strings 只要函数不修改中传递的 char ** 。否则,我看不到任何选择,只是将一切复制到一个新的字符**`结构 (见第二个例子)

  void old_func(char ** carray,size_t size)
{
for(size_t i = 0; i< size; ++ i)
std :: cout<< carray [i]< '\\\
';
}

int main()
{
std :: vector< std :: string> string {one,two,three};
std :: vector< char *> cstrings;

for(size_t i = 0; i< strings.size(); ++ i)
cstrings.push_back(const_cast< char *>(strings [i] .c_str )));

//不要改变这里的任何字符串
//使依赖于
的新数据结构无效//来自`c_str()`的返回值
//
//这不是C ++后的问题11

if(!cstrings.empty())
old_func(& cstrings [0 ],cstrings.size());示例2: 传入资料:

  void old_func(char ** carray,size_t size)
{
for size_t i = 0; i std :: cout < carray [i]< '\\\
';
}

int main()
{
std :: vector< std :: string> string {one,two,three};
char ** cstrings = new char * [string.size()];

for(size_t i = 0; i< strings.size(); ++ i)
{
cstrings [i] = new char [strings [i]。 size()+ 1];
std :: strcpy(cstrings [i],strings [i] .c_str());
}

//现在函数可以做它喜欢的(在边界内)
//与结构中传递

old_func(cstrings ,strings.size());

//清除内存

for(size_t i = 0; i< strings.size(); ++ i)
delete [] cstrings [一世];

delete [] cstrings;
}

EDIT5: C ++ 98 代码(thnx到BeyelerStudios)添加了一个解决方案,允许该函数修改传入的数据。


I have a vector<std::string> variable. I need to pass it onto a method which accepts char**as an input parameter.

how to do this ? If possible I need to pass a writable one.

Update 1: In a tool for creating a service method, i give parameters as std::vector, but it sets automatically the qualifier as &, which means my method definition generated by the tool will look as:

std::string SvcImpl::myMethodname ( const std::string par1, const std::vector<     std::string >& par2, const std::vector< std::string >& par3 )
{

}

This method gets called automatically with values in the patameter passed. Now from inside this method I'm going to call a method in a dll in a lib folder which looks like:

int method_to_be_called(char* par1, char ** par2, char ** par3, void* pRetValue);

for par1 --> I'm passing (char*)par1.c_str()

I need to know how to pass variables for par2 and par3 and for pRetValue. values for par2 and par3 are available in vector but the last parameter pRetValue is an output parameter that i need to return it as std::string.

sorry if i am very confusing or asking very basic questions.

解决方案

It is possible to solve the problem without copying out all the std::strings as long as the function does not modify the passed in char**. Otherwise I can see no alternative but to copy out everything into a new char**` structure (see second example).

void old_func(char** carray, size_t size)
{
    for(size_t i = 0; i < size; ++i)
        std::cout << carray[i] << '\n';
}

int main()
{
    std::vector<std::string> strings { "one", "two", "three"};
    std::vector<char*> cstrings;

    for(size_t i = 0; i < strings.size(); ++i)
        cstrings.push_back(const_cast<char*>(strings[i].c_str()));

    // Do not change any of the strings here as that will
    // invalidate the new data structure that relies on
    // the returned values from `c_str()`
    //
    // This is not an issue after C++11

    if(!cstrings.empty())
        old_func(&cstrings[0], cstrings.size());
}

EXAMPLE 2: If the function must modify the passed in data:

void old_func(char** carray, size_t size)
{
    for(size_t i = 0; i < size; ++i)
        std::cout << carray[i] << '\n';
}

int main()
{
    std::vector<std::string> strings { "one", "two", "three"};
    char** cstrings = new char*[strings.size()];

    for(size_t i = 0; i < strings.size(); ++i)
    {
        cstrings[i] = new char[strings[i].size() + 1];
        std::strcpy(cstrings[i], strings[i].c_str());
    }

    // Now the function can do what it likes (within the bounds)
    // with the passed in structure

    old_func(cstrings, strings.size());

    // clean up memory

    for(size_t i = 0; i < strings.size(); ++i)
        delete[] cstrings[i];

    delete[] cstrings;
}

EDIT5: Settled on a simpler solution for post C++98 code (thnx to BeyelerStudios) added a solution that allows the function to modify the passed in data.

这篇关于convert vector&lt; string&gt; into char ** C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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