如何将作为参数传递的长数组复制到std :: list [英] How to copy a long array passed as argument to a std::list

查看:81
本文介绍了如何将作为参数传递的长数组复制到std :: list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,



我正在尝试将一个long数组复制到std :: list或std:vector。



我有以下代码示例:



Hello Everyone,

I am trying to copy an array of long into a std::list or std:vector.

I have the following code sample:

#include "stdafx.h"
#include <iostream>
#include <list>
#include <iterator>

using namespace std;

class Channels
{
public:
void Configure(long channels[])
{
_channels <= channels; //..... copy channels to _channels
}

private:
   std::initializer_list<long> _channels{};
}







如果我走在正确的轨道上,请指点我。 br />


非常感谢您提前。

祝你好运。

MiQi



我尝试了什么:



我尝试过以下但不确定这是否正确:






Could you please point me if I am on the right track.

Thank you very much in advance.
Best regards.
MiQi

What I have tried:

I have tried the following but not sure if this is the correct way:

#include "stdafx.h"
#include <iostream>
#include <list>
#include <iterator>

using namespace std;

class Channels
{
public:
void Configure(std::initializer_list<long> chans, int size)
{
   _channels = std::initializer_list<long>{ chans };
}

private:
   std::initializer_list<long> _channels{};
}

void main()
{
   long channels[] {1, 3 , 5};
   Channels channel;
   Channel.Configure(channels);
}





示例代码编译愉快。



The sample code compiles happily.

推荐答案

一个简单的旧学校解决方案。

1.将数组的大小与数组一起传递;否则你不知道它的大小。

2.只需将它分配给一个成员变量。



这应该可以解决问题:
How about a simple old school solution.
1. Pass the size of the array along with the array; otherwise you do not know its size.
2. Just assign it to a member variable.

This should do the trick:
class Channels
{
public:
void Configure(long channels[], size_t size)
{
    _channels.assign(channels, channels+size);
}

private:
   std::vector _channels;
}


这篇关于如何将作为参数传递的长数组复制到std :: list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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