简体   繁体   English

将2D数组和字符串向量作为参数传递给函数

[英]Passing 2D array and vector of string as argument to function

I have written two function in which I am passing vector of string to a particular function (PrintStringVector) just to print content and in second function, passing the array of pointers to print the content.The first function is working fine but second one is giving error which is below the my code. 我编写了两个函数,其中我将字符串向量传递给特定函数(PrintStringVector)仅用于打印内容,而在第二个函数中,传递了指针数组以打印内容。第一个函数可以正常工作,但第二个函数可以我的代码下面的错误。

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

ERRORS: 错误:

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

In C++ you cannot have VLA (variable length arrays). 在C ++中,不能有VLA(可变长度数组)。 Since the size of the array arr is only known at runtime you cannot use it as a size of a fixed size array. 由于数组arr的大小仅在运行时才知道,因此您不能将其用作固定大小的数组的大小。 You should use new to allocate it. 您应该使用new来分配它。 Eg 例如

const char **arr = new const char*[n];

Also modify the function signature like this 还要像这样修改功能签名

void PrintStringArray(const char *arr[]){

or like this 或像这样

void PrintStringArray(const char **arr){

Finally remember to delete arr once you are finished with it. 最后,请记住在完成后删除arr

delete[] arr;
const char *arr[n]

This is not a valid declaration (unless n is a constant expression). 这不是有效的声明(除非n是常量表达式)。 C++ has no variable length arrays. C ++没有可变长度数组。

"But it works for me inside main !' “但是它在main内部对我有用!”

That's because g++ implements an extension to C++. 这是因为g ++实现了对C ++的扩展。 This extension does not seem compatible with C variable-length arrays, and is generally buggy. 此扩展似乎与C可变长度数组不兼容,并且通常是错误的。 Don't use it. 不要使用它。

"But how can I have comparable functionality?" “但是我怎么能拥有可比的功能呢?”

Use std::vector . 使用std::vector Do not use pointers, new[] and delete[] unless you know extremely well why you need these low-level primitives. 除非您非常清楚为什么需要这些低级原语,否则不要使用指针, new[]delete[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM