将二维数组和字符串向量作为参数传递给函数
Passing 2D array and vector of string as argument to function
我写了两个函数,其中我将字符串向量传递给特定函数 (PrintStringVector) 只是为了打印内容,在第二个函数中,传递指针数组来打印 content.The 第一个函数是工作正常,但第二个出现错误,低于我的代码。
#include <cmath>
#include <stdlib.h>
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
void PrintStringVector(vector<string> & v){
for(int i=0;i<v.size();i++){ cout<<v[i]<<endl;}
}
void PrintStringArray(const char *arr[n]){
for(int i=0;i<n;i++){ cout<<arr[i]<<endl;}
}
int main() {
vector<string> vec;
cin>>n;
const char *arr[n];
for(int i=0;i<n;i++){
string str;
cin>>str;
vec.push_back(str);
arr[i]=str.c_str();
}
PrintStringVector(vec);
PrintStringArray(arr);
return 0;
}
错误:
vinod@vinod-Inspiron-3537:~/Documents/hackerrank$ g++ passing_vector_of_string_or_passing_2d_array.cpp
passing_vector_of_string_or_passing_2d_array.cpp:17:35: error: expected ‘,’ or ‘...’ before ‘arr’
void PrintStringArray(const char* arr[n]){
^
passing_vector_of_string_or_passing_2d_array.cpp: In function ‘void PrintStringArray(const char*)’:
passing_vector_of_string_or_passing_2d_array.cpp:19:33: error: ‘arr’ was not declared in this scope
for(int i=0;i<n;i++){ cout<<arr[i]<<endl;}
^
passing_vector_of_string_or_passing_2d_array.cpp: In function ‘int main()’:
passing_vector_of_string_or_passing_2d_array.cpp:40:25: error: cannot convert ‘const char**’ to ‘const char*’ for argument ‘1’ to ‘void PrintStringArray(const char*)’
PrintStringArray(arr);
在 C++ 中,您不能使用 VLA(可变长度数组)。由于数组 arr
的大小仅在运行时已知,因此您不能将其用作固定大小数组的大小。您应该使用 new
来分配它。例如
const char **arr = new const char*[n];
也像这样修改函数签名
void PrintStringArray(const char *arr[]){
或者像这样
void PrintStringArray(const char **arr){
最后记得在完成后删除arr
。
delete[] arr;
const char *arr[n]
这不是有效的声明(除非 n
是常量表达式)。 C++ 没有变长数组。
“但它对我内部有效 main
!'
那是因为 g++ 实现了对 C++ 的扩展。此扩展似乎与 C 可变长度数组不兼容,并且通常存在错误。不要使用它。
"But how can I have comparable functionality?"
使用std::vector
。不要使用指针,new[]
和 delete[]
除非你非常清楚为什么你需要这些低级原语。
我写了两个函数,其中我将字符串向量传递给特定函数 (PrintStringVector) 只是为了打印内容,在第二个函数中,传递指针数组来打印 content.The 第一个函数是工作正常,但第二个出现错误,低于我的代码。
#include <cmath>
#include <stdlib.h>
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
void PrintStringVector(vector<string> & v){
for(int i=0;i<v.size();i++){ cout<<v[i]<<endl;}
}
void PrintStringArray(const char *arr[n]){
for(int i=0;i<n;i++){ cout<<arr[i]<<endl;}
}
int main() {
vector<string> vec;
cin>>n;
const char *arr[n];
for(int i=0;i<n;i++){
string str;
cin>>str;
vec.push_back(str);
arr[i]=str.c_str();
}
PrintStringVector(vec);
PrintStringArray(arr);
return 0;
}
错误:
vinod@vinod-Inspiron-3537:~/Documents/hackerrank$ g++ passing_vector_of_string_or_passing_2d_array.cpp
passing_vector_of_string_or_passing_2d_array.cpp:17:35: error: expected ‘,’ or ‘...’ before ‘arr’
void PrintStringArray(const char* arr[n]){
^
passing_vector_of_string_or_passing_2d_array.cpp: In function ‘void PrintStringArray(const char*)’:
passing_vector_of_string_or_passing_2d_array.cpp:19:33: error: ‘arr’ was not declared in this scope
for(int i=0;i<n;i++){ cout<<arr[i]<<endl;}
^
passing_vector_of_string_or_passing_2d_array.cpp: In function ‘int main()’:
passing_vector_of_string_or_passing_2d_array.cpp:40:25: error: cannot convert ‘const char**’ to ‘const char*’ for argument ‘1’ to ‘void PrintStringArray(const char*)’
PrintStringArray(arr);
在 C++ 中,您不能使用 VLA(可变长度数组)。由于数组 arr
的大小仅在运行时已知,因此您不能将其用作固定大小数组的大小。您应该使用 new
来分配它。例如
const char **arr = new const char*[n];
也像这样修改函数签名
void PrintStringArray(const char *arr[]){
或者像这样
void PrintStringArray(const char **arr){
最后记得在完成后删除arr
。
delete[] arr;
const char *arr[n]
这不是有效的声明(除非 n
是常量表达式)。 C++ 没有变长数组。
“但它对我内部有效 main
!'
那是因为 g++ 实现了对 C++ 的扩展。此扩展似乎与 C 可变长度数组不兼容,并且通常存在错误。不要使用它。
"But how can I have comparable functionality?"
使用std::vector
。不要使用指针,new[]
和 delete[]
除非你非常清楚为什么你需要这些低级原语。