vector<std::string> of commandline args
I have the need in my c++ program of modifying the command line args given certain conditions. This favors using a `vector<std::string>` instead of the current approach.
```c++
#include <subprocess.h>
#include <initializer_list>
#include <vector>
char *convert(const std::string & s)
{
char *pc = new char[s.size()+1];
std::strcpy(pc, s.c_str());
return pc;
}
int cmd(std::initializer_list<std::string> args)
{
std::vector<char*> vc;
std::transform(args.begin(), args.end(), std::back_inserter(vc), convert);
// vc.push_back(NULL);
struct subprocess_s subprocess;
int result = subprocess_create(&vc[0], 0, &subprocess);
if (0 != result) {
printf("error occurred.");
};
std::cout << "vc.size(): " << vc.size() << std::endl;
for ( size_t i = 0 ; i < vc.size() ; i++ )
std::cout << vc[i] << std::endl;
for ( size_t i = 0 ; i < vc.size() ; i++ )
delete [] vc[i];
return 0;
}
```
The problem I have is that if I `push_back(NULL)` to add a `NULL` sentinel I get an error and it doesn't work as expected.. Do I have to modify `subprocess.h` itself with this approach and just drop the sentinel requirement or is there a way of making something like the above work?
关闭于 2024-03-23 3 条评论