【问题标题】:C++ How to pass 2 arrays (of strings) to a function, and compare to a string [duplicate]C ++如何将2个数组(字符串)传递给函数,并与字符串进行比较[重复]
【发布时间】:2015-09-16 03:13:35
【问题描述】:

我是 C++ 初学者,所以请像 5 岁一样跟我说话。

这是我想要做的:

  • 将用户输入转换成字符串userInput

  • userInput 和 2 个数组(answersoutcomes)传递到函数 answerCheck

  • 比较 userInputanswers 数组

  • 如果匹配,则从outcomes输出字符串

  • 如果不匹配,循环,请求userInput

我用answersSize 输出answers 的大小。它输出 1 而不是预期的 2。

我不知道如何将数组中的信息传递给answerCheck 函数。

有什么建议吗?

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int question1();
bool answerCheck(string[], string[], string);

int main() {

    question1();
    system("pause");
    return 0;
}

int question1() {

    cout << "Do you want to go LEFT or RIGHT?" << endl;

    string answers[2] = { "left", "right" }; 
    string outcomes[2] = { "you went left", "you went right" }; 
    string userInput = ""; 

    getline(cin, userInput);

    // outputs correct size of answers array for testing ======
    int answersSize = sizeof(answers) / sizeof(string); 
    cout << "Correct size of answers: "<< answersSize << endl;
    // ========================================================

    answerCheck(answers, outcomes, userInput);

    return 0;
}


bool answerCheck(string answers[], string outcomes[], string userInput){ 

    int answersSize = sizeof(answers) / sizeof(string); 

    cout << "Size of answers: "<< answersSize << endl;

    for(int i=0; i < answersSize; i++){ 

        if(userInput.find(answers[i]) != string::npos){

            cout <<"\n" << outcomes[i] <<"\n" << endl;
            return true;
        }
    }

    cout << "Try putting in something else." << endl;
    return false;
}

【问题讨论】:

  • 永远不要在函数头中写string answers[],因为它看起来像一个数组,但它实际上不是一个数组——它实际上与string *answers的含义相同。 (是的,标准委员会在做出决定时一定觉得自己特别脑残)
  • @immibis 在第一个标准发布时,重要的是不要破坏太多现有代码,否则该标准不会被广泛采用。

标签: c++ arrays function


【解决方案1】:

问题出在这里:

int answersSize = sizeof(answers) / sizeof(string); 

如果打印出来,你会发现sizeof(answers)是一个指针的大小(4或8个字节),而不是整个数组的大小。您需要将数组大小作为函数参数传入,或者使用像 std::vector 这样的类类型,它以更 C++ 的方式封装它。

【讨论】:

  • 所以如果你使用一个字符串[],在通过一个函数传递它之后不可能用代码确定数组大小?
  • @Danny:正确。因为数组是通过指针传递的,一个指针不足以算出数组长度。
【解决方案2】:

对初学者的一般建议是使用 C++,例如 std::vector 类,而不是普通的 C 数组。所以在你的例子中,而不是

string answers[2] = { "left", "right" }; 

使用

std::vector<std::string> answers{ "left", "right" };

并声明你的函数

bool answerCheck(std::vector<string> const& answers, 
                 std::vector<string> const&outcomes, 
                 string const& userInput)

如果您正在阅读一本介绍性书籍,并且它首先介绍了 C 风格的代码,我会把它扔掉。 C ++的一个很好的介绍是例如https://isocpp.org/tour.

【讨论】:

  • 向量似乎是要走的路。我让它工作,但我没有在我的代码中使用 const& (我认为是引用?)。仍然有效。如果您想解释这一点,请随意。
  • @KevinB。该代码将在没有引用的情况下工作。当您按值传递对象时,编译器将创建一个副本并在函数中使用该副本。使用 const-reference 可以保存副本,因为您传递的是对无法修改的现有对象的引用。一般的经验法则是,如果您不存储或修改对象,则通过 const&amp; 传递对象。我建议你买一本好书,因为它也解释了这样的事情背后的原因。 C++ 之旅应该不错。
  • 谢谢!我将研究 C++ 之旅。
猜你喜欢
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多