C++ 使用C+中的指针将二维数组传递给函数+;
我一直在尝试将此数组传递给函数,但我不断得到, 错误C2664:'correctans':无法将参数2从'std::string[3][3]转换为'std::string**',don;Don’不要介意代码中那些愚蠢的问题,它只是随机测试的 代码:C++ 使用C+中的指针将二维数组传递给函数+;,c++,C++,我一直在尝试将此数组传递给函数,但我不断得到, 错误C2664:'correctans':无法将参数2从'std::string[3][3]转换为'std::string**',don;Don’不要介意代码中那些愚蠢的问题,它只是随机测试的 代码: #包括 #包括 使用名称空间std; 整数更正(字符串*arr1,字符串**arr2,整数*arr3,整数问题,整数选项) { 整数计数=0; INTANS; 对于(int i=0;i如果我没记错的话,您可以使用类似“string[][]]&rArr
#包括
#包括
使用名称空间std;
整数更正(字符串*arr1,字符串**arr2,整数*arr3,整数问题,整数选项)
{
整数计数=0;
INTANS;
对于(int i=0;i如果我没记错的话,您可以使用类似“string[][]]&rArray”的东西
(或者确切地说:string[3][3]&rArray),当然您也应该传递实际的维度作为参数
编译器接受了这一点:
字符串arr2[3][3]
作为参数。但我会尝试改进传递指针,而不是按值复制数组。因为您使用的是stl::string,所以也可以尝试vector。好吧,正如编译器所说,std::string[3][3]
不能转换为std::string**
你可以试试这个
int correctans(string *arr1, string (* arr2)[ 3 ], int *arr3, int questions, int choices)
还是这个
int correctans(string *arr1, string arr2[][ 3 ], int *arr3, int questions, int choices)
但更好的解决方案是使用std::vector
来吧
int correctans(string*arr1,string(&arr2)[3][3],int*arr3,int-questions,int-choices)`传递多维数组可能会非常混乱。我建议创建指向数组开头的单指针,并传递该指针:
#include <iostream>
#include <string>
using namespace std;
int correctans(string *arr1, string *arr2, int *arr3, int questions, int choices)
{
int count=0;
int ans;
for(int i=0; i<questions; i++)
{
cout << "Question #" << i+1;
cout << arr1[i] << endl;
for(int j=0; j<choices; j++)
cout << j+1 << arr2[i][j] << " ";
cout << "your answer:";
cin >> ans;
if(ans==arr3[i])
count++;
}
return count;
}
int main()
{
int correct;
string Questions[3]={"HowAreYou", "HowManyHandsDoYouHave", "AreYouCrazyOrCrazy"};
string Choices[3][3]={{"Banana", "Peanut", "Fine"},{"Five", "Two", "One"},{"I'mCrazy", "I'mCrazyBanana", "I'mDoubleCrazy"}};
int Answers[3]={3, 2, 3};
string* choicesPtr=&Choices[0][0];
correct=correctans(Questions, choicesPtr, Answers, 3, 3);
cout << "You have " << correct << " correct answers" <<endl;
return 0;
}
#包括
#包括
使用名称空间std;
整数更正(字符串*arr1、字符串*arr2、整数*arr3、整数问题、整数选项)
{
整数计数=0;
INTANS;
对于(int i=0;i但我有一个问题,如果我不知道2d数组的大小,我可以使用什么来代替[3][3]因为我尝试只从函数中更改数字,但它不起作用。我希望能够将其设置为任何大小。这可能吗?@wildbegginer:您可以使用correctans
template函数。template correctans(…,string(&arr)[size][size],…)
。但是如果您在编译时不知道大小,那么它就不起作用。第二个可行,但没有使用指针传递。不过,谢谢@kotlomoy@WildBegginner实际上stringarr2[][3]
隐式转换为string(*arr2)[3]
。因此,对于编译器来说,两者都是相同的。顺便说一句,第一个现在可以工作了(我在第一版文章中漏掉了括号)
#include <iostream>
#include <string>
using namespace std;
int correctans(string *arr1, string *arr2, int *arr3, int questions, int choices)
{
int count=0;
int ans;
for(int i=0; i<questions; i++)
{
cout << "Question #" << i+1;
cout << arr1[i] << endl;
for(int j=0; j<choices; j++)
cout << j+1 << arr2[i][j] << " ";
cout << "your answer:";
cin >> ans;
if(ans==arr3[i])
count++;
}
return count;
}
int main()
{
int correct;
string Questions[3]={"HowAreYou", "HowManyHandsDoYouHave", "AreYouCrazyOrCrazy"};
string Choices[3][3]={{"Banana", "Peanut", "Fine"},{"Five", "Two", "One"},{"I'mCrazy", "I'mCrazyBanana", "I'mDoubleCrazy"}};
int Answers[3]={3, 2, 3};
string* choicesPtr=&Choices[0][0];
correct=correctans(Questions, choicesPtr, Answers, 3, 3);
cout << "You have " << correct << " correct answers" <<endl;
return 0;
}