如何将字符串数组传递给C ++中的函数 [英] how do I pass a string array to a function in C++

查看:96
本文介绍了如何将字符串数组传递给C ++中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这段代码



i tried this code

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace::std;
// The called function //
void rando1(int a[])
{
    for(int j=0;j<5;j++)
    {
    
        cout<<a[j]<<endl;
        cout<<endl;
    }
}

main()
{
    int arr[4];
    for(int i=0;i<5;i++)
    {
        cout<<"Enter 5 numbers: ";
        cin>>arr[i];
    }
    cout<<endl;
    // The calling function //
    rando1(arr);
    getch();
}









工作原理



但我也尝试将数据类型int更改为字符串,但我没有工作。如何将字符串数组传递给函数?



感谢您帮助





it works

but i also tried changing the data type int into a string but i didnt work. how can i pass string arrays to a function?

Thank you for helping

推荐答案

您正在使用此代码破坏内存。 4元素数组的元素位于[0..3]范围内,而不是[0..5]。传递字符串与传递int完全相同,只需确保函数参数类型正确,例如

You are corrupting memory with this code. The elements of a 4 element array lie in the range [0..3] not [0..5]. And passing strings is exactly the same as passing ints, just make sure the function parameter type is correct, e.g.
void rando1(string a[])
{
    for(int j = 0; j < 4; j++)
    {
        cout << a[j] << endl;
        cout << endl;
    }
}


现代 C ++ 可以很有表现力:



Modern C++ can be quite expressive:

#include <iostream>
#include <string>
#include <array>
using namespace std;

template <typename T, size_t N> void show_array( array <T,N> & a)
{
  for (const auto & x : a) cout << x << endl;
}

int main()
{

  array<int,3> ai = { -3 ,17, 0};
  array<string,2> as= {"foo", "bar"};

  show_array(ai);
  show_array(as);
}







更多(更多)传统方法:




A more (more) traditional approach:

#include <cstddef>
#include <iostream>
#include <string>
using namespace std;

void show_int_array(int ai[], size_t size)
{
  for (size_t n = 0; n < size; ++n)
    cout << ai[n] << endl;
}
void show_string_array(string as[], size_t size)
{
  for (size_t n = 0; n < size; ++n)
    cout << as[n] << endl;
}


int main()
{
  int ai[] = {3, -5, 7};
  string as[] = { "foo", "bar" };

  show_int_array(ai, sizeof(ai)/sizeof(ai[0]));
  show_string_array(as, sizeof(as)/sizeof(as[0]));
}





show_int_array 和<$ c的代码冗余$ c> show_string_array 立即建议使用模板:





The code redundancy of show_int_array and show_string_array immediately suggests the usage of a template:

#include <cstddef>
#include <iostream>
#include <string>
using namespace std;

template <typename T> void show_array(T a[], size_t size)
{
  for (size_t n = 0; n < size; ++n)
    cout << a[n] << endl;
}

int main()
{
  int ai[] = {3, -5, 7};
  string as[] = { "foo", "bar" };

  show_array(ai, sizeof(ai)/sizeof(ai[0]));
  show_array(as, sizeof(as)/sizeof(as[0]));
}





'极简主义'方法



'Minimalist' approach

#include <iostream>
#include <string>
using namespace std;

int main()
{
  int ai[] = {3, -5, 7};
  string as[] = { "foo", "bar" };

  for (auto x : ai) cout << x << endl;
  for (auto s : as) cout << s << endl;
}


这篇关于如何将字符串数组传递给C ++中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆