Click here to Skip to main content
14,084,843 members
Rate this:
 
Please Sign up or sign in to vote.
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:

#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:

#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 3 days ago

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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;
}
   
Comments
SuperMiQi 3 days ago
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190518.1 | Last Updated 15 May 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100