c++ - 将字符串数组传递给函数

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

我正在尝试创建一个程序,该程序使用类、数组和函数来显示有关两个学生的信息(姓名、ID#、已注册的类(class))。我苦苦挣扎的部分是将数组传递给函数。我该怎么做?

#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;
}

最佳答案

在 C++ 中传递可变长度列表的常用方法是使用 std::vectorvector 是一个单一的对象,您可以轻松地将其传递给函数,复制(或引用)其内容。如果您熟悉 Java,它基本上就是一个 ArrayList。这是一个例子:

#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);

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

void processStrings(string* arr, int len) {
  for (int i = 0; i < len; i++) {
    string str = arr[i];
    //...
  }
}

string array[] = {"foo","bar","baz"};
processStrings(array, 3); // you could also replace 3 with sizeof(array)

像这样传递原始数组,尤其是如果您想随后将数组复制到一个对象中,可能会很痛苦。 C 和 C++ 中的原始数组只是指向列表第一个元素的指针。与 Java 和 JavaScript 等语言不同,它们不跟踪它们的长度,并且您不能只将一个数组分配给另一个数组。 std::vector 封装了“事物列表”的概念,并且通常更直观地用于该目的。

生活课:使用 std::vector。

编辑:有关使用构造函数更清晰地初始化对象的示例,请参阅@nathanesau 的回答。 (但不要复制粘贴,自己写!那样你会学得更快。)

关于c++ - 将字符串数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38734599/

相关文章:

javascript - 简单练习函数显示NaN

C++成员初始化列表问题

C++ fstream 混淆

Java - 使用扫描仪从文本文件读取到二维字符数组

c++ - 需要将 ifstream 输入文件转换为我可以循环和解密的格式

没有第一个的 C++ 可变参数

c++ - 将鼠标位置转换为方向和后退

c++ - static int 执行了多少次?

c++ - 是否可以在 C++ 中创建对象数组队列?

javascript - 按小于号时调用函数 不按逗号时调用函数