C ++:从stringstream到char ** [英] C++: From stringstream to char**
问题描述
我有一个类 parse(int argc,char * argv [])
函数,我必须使用它来设置一个对象的期望状态。我使用 stringstream
从gui参数,然后我试图将它们转换为char **将它们传递给函数。这是我得到的:
std :: stringstream sstream;
sstream<< -clip< < min_x_entry.get_text()
<< < max_x_entry.get_text(); //等等。
std :: cout<< sstream.str(); // All looks good here
std :: vector< std :: string> args;
std :: vector< char *> argv;
std :: string arg;
while(sstream>> arg)
{
args.push_back(arg);
argv.push_back(const_cast< char *>(args.back()。c_str()));
}
argv.push_back(0);
int argc = args.size();
for(int i = 0; i std :: cout< & argv [0] [i]; //这会输出垃圾
my_object.parse(argc,& argv [0])//这会失败
我缺少什么?
一个问题是重新分配 args
向量作为
push_back()
将根据需要增加向量的大小:
如果新size()不大于capacity (),没有迭代器或引用无效。 否则所有的迭代器和引用都会失效。
argv
vector存储指向 args
中元素内部的指针,因此这些指针将无效。
解决方案是先创建 args
向量,然后再创建 argv
向量:
while(sstream>> arg)args.push_back(arg);
循环打印出
for(auto i = args.begin(); i!= args.end(); i ++)
{
argv.push_back(const_cast< char *> i-> c_str()));
}
argv.push_back(0);
argv
字符串不正确。这:& argv [0] [i]
是
char *
,但从i
th元素的argv
中的第一个条目。例如,如果argv
中的第一个c字符串string
:& argv [0] [1]是tring
& argv [0] [2]是ring
更改为:
for(int i = 0; i
std :: cout < argv [i]<< std :: endl; //添加'endl'来刷新'cout'。
I have a class with
parse(int argc, char* argv[])
function which I have to use to set a desired state of an object. I'm taking the parameters from the gui usingstringstream
and then I'm trying to convert them to char** to pass them to the function. Here's what I've got:std::stringstream sstream; sstream << "-clip" << " " << min_x_entry.get_text() << " " << max_x_entry.get_text(); // etc. std::cout << sstream.str(); // All looks good here std::vector<std::string> args; std::vector<char*> argv; std::string arg; while (sstream >> arg) { args.push_back(arg); argv.push_back(const_cast<char*>(args.back().c_str())); } argv.push_back(0); int argc = args.size(); for (int i = 0; i < argc; ++i) std::cout << &argv[0][i]; // This outputs garbage my_object.parse(argc, &argv[0]) // And this fails
What am I missing? Is there a better way of achieving this?
解决方案A problem would be reallocation of the
args
vector aspush_back()
will grow the size of the vector if required:If new size() is not larger than capacity(), no iterators or references are invalidated. Otherwise all iterators and references are invalidated.
The
argv
vector is storing pointers to the internals of elements inargs
, so these would be invalidated.A solution would be to create the
args
vector first then create theargv
vector afterwards:while (sstream >> arg) args.push_back(arg); for (auto i = args.begin(); i != args.end(); i++) { argv.push_back(const_cast<char*>(i->c_str())); } argv.push_back(0);
The
for
loop that prints out theargv
strings is incorrect. This:&argv[0][i]
is a
char*
but starts fromi
th element of the first entry inargv
. For example, if the first c-string inargv
was"string"
:&argv[0][1] is "tring" &argv[0][2] is "ring"
change to:
for (int i = 0; i < argc; i++) std::cout << argv[i] << std::endl; // Added 'endl' to flush 'cout'.
这篇关于C ++:从stringstream到char **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!