Click here to Skip to main content
15,918,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

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

I have the following code sample:

C++
#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{};
}



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:

C++
#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.
Posted
Updated 15-May-19 10:30am

1 solution

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;
}
 
Share this answer
 
Comments
SuperMiQi 15-May-19 17:52pm    
Thank you very much. It was indeed a good approach.
I will now post my entire code set to see if I am on the right path. It works but maybe am I missing important keys.
Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900