将字符串数组传递给函数

时间:2016-08-03 04:46:33

标签: c++ arrays function visual-c++ c++14

我正在尝试创建一个程序,它使用类,数组和函数来显示有关两个学生的信息(Name,id#,注册的类)。我正在努力的部分是将数组传递给函数。我该怎么做?

#include <string>
#include <iostream>
#include <iomanip>
using namespace std;

class Student // Student class declaration.
{
private:
  string name;
  int id;
  string classes;
  int arraySize;

public:
  void setName(string n)
  {
    name = n;
  }
  void setId(int i)
  {
    id = i;
  }
  void setClasses(string c, int num)
  {
    classes = c;
    arraySize = num;
  }
  string getName()
  {
    return name;
  }
  int getId()
  {
    return id;
  }
  void getClasses()
  {
    for (int counter=0; counter <arraySize; counter++) {

      cout << classes[counter] << endl;
    }

  }

};

int main()
{
  //Student 1
  string s1Name = "John Doe";
  int s1Id = 51090210;
  int const NUMCLASSES1 = 3;
  string s1Classes[NUMCLASSES1] = {"C++","Intro to Theatre","Stagecraft"};
  //Student 2
  string s2Name = "Rick Harambe Sanchez";
  int s2Id = 666123420;
  int const NUMCLASSES2 = 2;
  string s2Classes[NUMCLASSES2] = {"Intro to Rocket Science","Intermediate Acting"};
  //

  Student info;

  info.setName(s1Name);
  info.setId(s1Id);
  //info.setClasses(s1Classes, NUMCLASSES1);
  cout << "Here is Student #1's information:\n";
  cout << "Name: " << info.getName() << endl;
  cout << "ID: " << info.getId() << endl;
  //cout << "Classes: " << info.getClasses() << endl;


  info.setName(s2Name);
  info.setId(s2Id);
  // info.setClasses(s2Classes, NUMCLASSES1);
  cout << "\n\nHere is student #2's information:\n";
  cout << "Name: " << info.getName() << endl;
  cout << "ID: " << info.getId() << endl;
  //cout << "Classes: " << info.getClasses() << endl;



  return 0;
}

4 个答案:

答案 0 :(得分:1)

在C ++中传递可变长度列表的常用方法是使用ListModelDim R1, R2, R3 As Range Set R1 = Range("E3") Set R2 = Range("G3") Set R3 = Range("I3") If (R1 = R2 Or R1 = R3) Then MsgBox "Room Already Used!" With Application .EnableEvents = False .Undo .EnableEvents = True End With End If 是一个单个对象,您可以轻松地将其传递给函数,复制(或引用)其内容。如果您熟悉Java,它基本上是std::vector。这是一个例子:

vector

如果不想使用标准库,您可以传递数组C风格。这涉及将指针传递给数组的第一个元素,以及数组的长度。例如:

ArrayList

像这样传递原始数组,特别是如果你想将数组复制到一个对象中,可能会很痛苦。 C&amp;中的 原始数组 C ++只是指向列表第一个元素的指针。与Java和JavaScript等语言不同,它们不会跟踪它们的长度,也不能只将一个数组分配给另一个数组。 #include <vector> #include <string> using namespace std; class foo { private: vector<string> myStrings; public: void setMyStrings(vector<string> vec) { myStrings = vec; } } //... foo myObj; vector<string> list = {"foo","bar","baz"}; myObj.setMyStrings(list); 封装了&#34;事物列表的概念&#34;并且通常更直观地用于此目的。

生活课:使用std :: vector。

编辑:请参阅@ nathanesau的答案,了解使用构造函数更清晰地初始化对象的示例。 (但不要复制粘贴,自己写下来!你会以这种方式学得更快。)

答案 1 :(得分:0)

在Student类的私有变量中,存储一个字符串: String classes; 你应该在哪里存储一个字符串数组,如: String classes[MAX_NUM_CLASSES];

然后在set classes函数中,传入一个字符串数组作为第一个参数,所以应该是:

void setClasses(string[] c, int num)

{

classes = c; //not sure if simply setting them equal will work, rather copy entire array using a for loop

 arraySize = num;

}

这应该指向正确的方向

另外,使用std::vector代替string[],会更容易。

答案 2 :(得分:0)

您可以像这样将any_data_type数组传递给function

void foo(data_type arr[]);

foo(arr); // If you just want to use the value of array 
foo(&arr); // If you want to alter the value of array.

答案 3 :(得分:0)

使用std::vector。此外,不要添加您不需要的功能。以下是使用std::vector

的示例
#include <string>
#include <iostream>
#include <vector>

using std::string;
using std::vector;

class Student // Student class declaration.
{
private:

    vector<string> classes;
    string name;
    int id;

public:

    Student (const vector<string> &classesUse, string nameUse, int idUse) :
        classes (classesUse),
        name    (nameUse),
        id      (idUse)
    {
    }

    void print ()
    {
        std::cout << "Name:    " << name << std::endl;
        std::cout << "Id:      " << id << std::endl;
        std::cout << "Classes: ";

        for (int i = 0; i < classes.size (); i++)
        {
            if (i < classes.size () - 1)
            {
                std::cout << classes[i] << ", ";
            }
            else
            {
                std::cout << classes[i] << std::endl;
            }
        }

        std::cout << std::endl;
    }
};

int main()
{
    Student John ({"C++","Intro to Theatre","Stagecraft"},
                  "John",
                  51090210);

    John.print ();

    Student Rick ({"Intro to Rocket Science","Intermediate Acting"},
                  "Rick",
                  666123420);

    Rick.print ();

    return 0;
}


Name:    John
Id:      51090210
Classes: C++, Intro to Theatre, Stagecraft

Name:    Rick
Id:      666123420
Classes: Intro to Rocket Science, Intermediate Acting