如何将值附加到命令行参数数组? [英] How to append a value to the array of command line arguments?

查看:128
本文介绍了如何将值附加到命令行参数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有入口点

My application has entry point

int main(int argc, char *argv[])
{

}

我需要将*argv数组扩展为n+1并附加一个值.例如,我需要附加"-app_ver".

I need to extend *argv array to n+1 and append a value. For example, I need to append "-app_ver".

我是C ++(具有Java背景)的新手.我知道我无法更改数组大小,因此我需要任何解决方案(任何复制数组的方法,等等)

I'm a newcomer in C++ (with Java background). I know that I can't change array size, so I need any solution (any approach copying array, etc.)

推荐答案

就像cbuchart所说,您必须创建一个新数组或一个向量. 使用向量和字符串对象比使用char *和array更简单.

Like cbuchart says, you have to create a new array or maybe a vector. Using vector and string object can be more simple than char* and array.

示例:

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    vector<string> list;
    for ( int i = 1 ; i < argc ; i++){
        string tmp (argv[i]);
        list.push_back(tmp); // add all arguments to the vector
    }

    cout << "number of given arguments : " << list.size() << endl;

    list.push_back("-app_ver"); // add one string to the vector

    for ( int i = 0 ; i < list.size() ; i++){
        cout << list[i] << endl; // acces data of the vector
    }

}

这篇关于如何将值附加到命令行参数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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