从命令行参数指定数组 [英] Specify Array from Command Line Argument

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

问题描述

我意识到从命令行传递的每个参数都作为字符串存储在'char * argv []'中。

I realize that each argument passed from the command line is stored as a string in 'char *argv[]'

我想传递

$ progname array[500]


b $ b

然后从argv [1]中拉出字符串,然后根据读入的内容指定一个数组。

and pull the string from argv[1] subsequently specifying an array based on what I read in.

任何想法?

推荐答案

从你的新评论,我会尽力回答这个问题。我正在做这些的大部分给你一个可能性,我的代码将有一些临时字符串解析和没有太多的错误检查,它只是给你一个想法,如何做到。

Well from your new comment, I will try to answer the question. I am doing most of this to show you one possibility, my code will have some makeshift string parsing and not much error checking, it's just to give you an idea of how you can do it.

如果你想让用户指定你想要的数组的大小,那就是可行的。

If you wanted the user to specify the size of an array you wanted to make, that is doable.

#include <iostream>
#include <cstdlib>

int main(int argc, char **argv)
{
   if (argc < 2) // the program's name is the first argument
   {
      std::cerr << "Not enough arguments!\n";
      return -1;
   }

   int arraySize = std::atoi(argv[1]);

   char *cArray = new char[arraySize];

   delete [] cArray;

   return 0;
}



如果你运行上面的程序并传递参数 500 ,它将分配一个内存块以存储高达500 * sizeof(char)

If you run the above program and pass it argument 500, it would allocate a memory block to store up to 500 * sizeof(char).

如果你希望用户实际提供数组的名称及其大小,那就有点不同了 - 你不能在运行时创建变量名。你可以做的是将字符串映射到数组。

If you wanted the user to actually give you the name of the array and its size, that's a little different - you can not create a variable name at runtime. One thing you could do is map strings to arrays.

这里是一个程序,它将接受一个由 hello [100] / code>并创建 hello 到100个元素的空间块的指针。

Here's a program that would accept an argument list consisting of arguments like hello[100] and create a mapping of hello to a pointer to a block of space for 100 elements.

#include <iostream>
#include <cstdlib>
#include <map>
#include <string>
#include <boost/shared_ptr.hpp>

int main(int argc, char **argv)
{
    std::map<std::string, boost::shared_ptr<char> > arrayMap;

    for(int i = 1; i < argc; ++i)
    {
       // arguments are like 'hello[500]', 'array[100]
       std::string s = argv[i];
       std::size_t posOpen = s.find("[");
       std::size_t posClose = s.find("]");

       std::string name = s.substr(0, posOpen);

       std::string size = s.substr(posOpen, posClose);

       // remove the '[' and ']' characters
       size.erase(0, 1);
       size.erase(size.length()-1, 1);

       std::cout << name << " " << size << std::endl; 

       // allocate our new array and wrap a shared_ptr around it
       boost::shared_ptr<char> cptr(new char[std::atoi(size.c_str())]);

       arrayMap[name] = cptr;

    }

    // now you can access the arrays 'by name'

    return 0;
}

如果你运行上面的代码并传递一个参数列表,例如 hello [500] array [1000] somelist [100] ,它将创建3个堆分配的块和映射名称到它们:hello / code>将映射到指向 500 * sizeof(char)array将映射到 1000 * sizeof(char)的块的指针,并且somelist将映射到指向 100 * sizeof(char)字节的块的指针。

If you ran the above code and passed it a list of arguments, such as hello[500] array[1000] somelist[100], it would create 3 heap-allocated blocks and map names to them: "hello" would map to a pointer to a block of 500 * sizeof(char), "array" would map to a pointer to a block of 1000 * sizeof(char), and "somelist" would map to a pointer to a block of 100 * sizeof(char) bytes.

上面的代码有一个问题虽然 - 它不跟踪分配给每个指针的指向块的字节数,这使它相当没有为您。一个替代方法是将名称映射到 boost :: array< Typename T,std :: size_t n> ,这将是boost的固定大小 array 类,它跟踪其大小并具有 size()成员函数。

There is a problem with that code above though - it does not keep track of the number of bytes allocated for each pointer's pointed-to block, which makes it rather worthless for you. One alternative would be to map the names to boost::array<Typename T, std::size_t n>, which will be boost's fixed-size array class that keeps track of its size and has a size() member function.

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

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