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

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

问题描述

我的应用程序有入口点

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 说的,你必须创建一个新的数组或者一个向量.使用vector和string对象可以比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天全站免登陆