如何在C ++中传递一个字符串数组

时间:2014-04-23 21:37:25

标签: c++ arrays

我正在尝试为学校项目编写命令行界面。我有一个driver.cpp来运行命令行,并将命令解析为commandInterpreter的字符串数组。当我尝试调试时,看起来只有当我调用interpreter->interpret(rowData, length);时才传递第一个字符串如果我键入" import p filename"在命令行中,看起来args只包含字符串" import"。我期待它被传递{" import"," p"," filename"}。我是否错误地传递或访问了字符串数组?

Driver.cpp

#include "CommandInterpreter.h"
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() 
{
   CommandInterpreter * interpreter = new CommandInterpreter();

   while ( !interpreter->terminate() )
   {
      string cmd;
      getline(cin, cmd);

      //Parse command args to lowercase
      int i = 0;
      while(cmd[i])
      {
         char c = cmd[i];
         putchar(tolower(c));
         i++;
      }
      //Parse command line into command and parameters.
      //Space delimited.
      stringstream ss(cmd);
      string item;
      string * rowData = new string[100];
      int length = 0;
      while (getline(ss, item, ' ')) {
         rowData[length] = item;
         length++;
      }

      //Interpret command
      interpreter->interpret(rowData, length);
      //Print feedback
      cout << interpreter->feedback() << '\n';
   }

   delete interpreter;
   return 0;
}

CommandInterpreter.h

#ifndef __COMMANDINTERPRETER_H
#define __COMMANDINTERPRETER_H

#include <string>
#include "ProjectController.h"
#include "LocationController.h"
#include "VolunteerController.h"
#include "ScheduleGenerator.h"
#include "VolunteerScheduler.h"

class ProjectController;
class LocationController;
class VolunteerController;
class ScheduleGenerator;
class VolunteerScheduler;

using namespace std;

class CommandInterpreter
{
public:
   CommandInterpreter(void);
   ~CommandInterpreter(void);

   //---------------------------------------------------------------------
   // Tells the driver when to teminate the program.
   //---------------------------------------------------------------------
   bool terminate(void);

   //---------------------------------------------------------------------
   // Interprets the given array of commands.
   //---------------------------------------------------------------------
   bool interpret(string * commands, int length);

   //---------------------------------------------------------------------
   // Provides feedback for the last command run.
   //---------------------------------------------------------------------
   string feedback(void);

private:
   bool exitFlag;
   string handback;

   ProjectController * PC;
   LocationController * LC;
   VolunteerController * VC;
   ScheduleGenerator * SG;
   VolunteerScheduler * VS;

public:
   //---------------------------------------------------------------------
   // Interprets a save command.
   //---------------------------------------------------------------------
   bool save(char arg0);

   //---------------------------------------------------------------------
   // Interprets an import command
   //---------------------------------------------------------------------
   bool import(char arg0, string filename);

   //---------------------------------------------------------------------
   // Interprets a print command.
   //---------------------------------------------------------------------
   bool print(char arg0, char arg1);

   //---------------------------------------------------------------------
   // Interprets a schedule command.
   //---------------------------------------------------------------------
   bool schedule(char arg0);

   //---------------------------------------------------------------------
   // Interprets a help command. Provides a list of commands in feedback.
   //---------------------------------------------------------------------
   bool help(void);
};

#endif

CommandInterpreter.cpp

#include "CommandInterpreter.h"

CommandInterpreter::CommandInterpreter(void)
{
   exitFlag = false;
   handback = "";
   PC = new ProjectController();
   LC = new LocationController();
   VC = new VolunteerController();
   SG = new ScheduleGenerator();
   VS = new VolunteerScheduler();
}


CommandInterpreter::~CommandInterpreter(void)
{
   delete PC;
   delete LC;
   delete VC;
   delete SG;
   delete VS;
}


bool CommandInterpreter::terminate(void)
{
   return this->exitFlag;
}


bool CommandInterpreter::interpret(string * args, int length)
{
   //args should be an array of strings like {"import","p","filename"}
   //Debug only shows "import"

   string cmd = args[0];
   handback = "";

   if(cmd == "exit")
   {
      exitFlag = true;
   }
   else if(cmd == "save")
   {
      this->save(args[1][0]);
   }
   else if(cmd == "import")
   {
      this->import(args[1][0], args[2]);
   }
   else if(cmd == "print")
   {
      if(length == 2)
         this->print(args[1][0], '0');
      else
         this->print(args[1][0], args[2][0]);
   }
   else if(cmd == "schedule")
   {
      this->schedule(args[1][0]);
   }
   else if(cmd == "help")
   {
      this->help();
   }
   else
   {
      this->handback = "Invalid Command. Type help to see commands.";
      return false;
   }

   return true;
}


string CommandInterpreter::feedback(void)
{
   return this->handback;
}


bool CommandInterpreter::save(char arg0)
{
   bool success = true;

   switch (arg0)
   {
   case 'p':
      // PC->save();
      handback = "Saved project schedule.";
      break;
   case 'v':
      // VC->save();
      handback = "Saved volunteer schedule.";
      break;
   default:
      // Uh-oh
      handback = "Invalid argument for save: " + arg0;
      success = false;
      break;
   }
   return success;
}

bool CommandInterpreter::import(char arg0, string filename)
{
   bool success = true;
   switch (arg0)
   {
   case 'p':
      PC->importCSV(filename);
      handback = "Imported projects file: " + filename;
      break;
   case 'l':
      LC->importCSV(filename);
      handback = "Imported locations file: " + filename;
      break;
   case 'v':
      VC->importCSV(filename);
      handback = "Imported volunteers file: " + filename;
      break;
   default:
      success = false;
      handback = "Invalid argument for import command: " + arg0;
      break;
   }
   return success;
}


bool CommandInterpreter::print(char arg0, char arg1)
{
   bool success = true;
   switch (arg0)
   {
   case 'p':
      // PC->print()
      // or project schedule print
      // depending on arg1
      if(arg1 == '0')
         handback = PC->getProjectStrings();
      else
         ;   //Print project schedule here.
      break;
   case 'l':
      // LC->print()
      break;
   case 'v':
      // VC->print()
      // or volunteer schedule print
      // depending on arg1
      break;
   default:
      success = false;
      handback = "Invalid argument for print command: " + arg0;
      break;
   }
   return success;
}


bool CommandInterpreter::schedule(char arg0)
{
   bool success = true;
   switch (arg0)
   {
   case 'p':
      // SG->generate()
      handback = "Project schedule generated.";
      break;
   case 'v':
      // VS->generate()
      handback = "Volunteer schedule generated.";
      break;
   default:
      success = false;
      handback = "Invalid argument for schedule command: " + arg0;
      break;
   }
   return success;
}


bool CommandInterpreter::help(void)
{
   return false;
}

1 个答案:

答案 0 :(得分:1)

哇,这么多问题,时间太短了......

不必要的动态内存分配
CommandInterpreter * interpreter = new CommandInterpreter(); 你在没有动态分配的情况下使用std::string,为什么这个呢? 此外,如果必须动态分配,则应使用具有指针和内存管理处理功能的unique_ptrboost::scoped_ptr

如果对象对于局部变量是大的,则将其声明为static

将'\ 0'用于std :: string终止
'\ 0'是C风格的字符串终止符。当您使用getline时,它会使用文本填充std::string对象。 std::string不使用nul字符进行字符串终止。更改while循环以使用迭代器或使其成为for循环的长度:

for (unsigned int i = 0; i < cmd.length(); ++i)
{
  cmd[i] = std::tolower(cmd[i]);
}

但是为什么在使用std::transform转换所有字符时使用循环。在网络或StackOverflow中搜索“c ++ transform tolower”。

通过std::vector而非数组
如您所知,将数组传递给函数很困难,尤其是语法。向量非常简单:

std::vector<std::string> commands;  // Look Ma, no *new* operator.
CommandInterpreter(commands);

传递数组
请记住,阵列名称可以衰减到第一个插槽的地址。此外,您需要传递容量以及数组中的元素数量:

void CommandInterpreter(string * array, unsigned int items_in_array, unsigned int array_capacity);

或者

void CommandInterpreter(std::vector<std::string>& array); // Much simpler using vector.

文件命名惯例
您为C ++标头使用不同的扩展名。历史上,“。h”后缀用于C语言头文件。例如,以下内容在C语言“.h”文件中有效:

typedef struct class
{
  unsigned int cout;
  double       private;
} class_t;

编译C语言时,您的头文件会做一些奇怪的事情。

某些约定使用“.hh”或“.hpp”来表示头文件是用于C ++的。

使用变量名提高可读性
两个字母变量不可读。选择更长的变量名称。如果您选择两个字母缩写,因为它太长而无法键入,请选择键盘类。

使用初始化列表
您可以在初始化列表中使用new运算符,这会让我想起......

检查错误的内存分配
需要检查每个动态内存分配是否成功。这可以通过检查指针为NULL或捕获异常来执行。还要验证您的实现是否new引发异常。有些编译器允许您关闭异常抛出。

并非每个功能都需要在对象中
一些面向对象的语言强制执行函数必须在类中的规则。 C ++语言不同,您可以在类之外使用称为“独立功能”的功能。试试吧!

现在够了......