Is there a way of specifying the contents of a vector in the constructor
when the elements are not identical?
I would like to be able to construct a static const vector that contains
pre-determined values.
class CFoo
{
static const std::vector<intm_foo;
static const int *const m_bar;
....
};
const std::vector<intCFoo::m_foo({1, 2, 3});
const int *const CFoo::m_bar = {10, 20, 30};
As I want a static member, it needs to be all done through the constructor.
Would it be easier just to declare a const array as in m_bar?
thanks
dan 7 1769
On Fri, 02 May 2008 11:24:55 +0100, Dan Smithers wrote:
Is there a way of specifying the contents of a vector in the constructor
when the elements are not identical?
I would like to be able to construct a static const vector that contains
pre-determined values.
class CFoo
{
static const std::vector<intm_foo;
static const int *const m_bar;
...
};
const std::vector<intCFoo::m_foo({1, 2, 3});
const int *const CFoo::m_bar = {10, 20, 30};
As I want a static member, it needs to be all done through the
constructor.
std::vector has a constructor that takes a range (i.e. a first and last
iterator) as parameters. These iterators can be pointers, so I suppose
you could use that. Eg.:
#include<vector>
int main()
{
const int a[] = {10,20,30};
const int* const p = a;
const std::vector<intv(p,p+3);
}
works as expected.
Would it be easier just to declare a const array as in m_bar?
Only you know the answer to that.
--
Lionel B
Dan Smithers schrieb:
Is there a way of specifying the contents of a vector in the constructor
when the elements are not identical?
You could use the Boost Assignment library. http://www.boost.org/doc/libs/1_35_0...doc/index.html
I would like to be able to construct a static const vector that contains
pre-determined values.
class CFoo
{
static const std::vector<intm_foo;
static const int *const m_bar;
...
};
const std::vector<intCFoo::m_foo({1, 2, 3});
const int *const CFoo::m_bar = {10, 20, 30};
const std::vector<intCFoo::m_foo(list_of(1)(2)(3));
--
Christian Hackl
On Fri, 02 May 2008 14:21:41 +0200, Christian Hackl wrote:
Dan Smithers schrieb:
>Is there a way of specifying the contents of a vector in the constructor when the elements are not identical?
You could use the Boost Assignment library.
http://www.boost.org/doc/libs/1_35_0...doc/index.html
>I would like to be able to construct a static const vector that contains pre-determined values.
class CFoo { static const std::vector<intm_foo;
static const int *const m_bar; ... };
const std::vector<intCFoo::m_foo({1, 2, 3}); const int *const CFoo::m_bar = {10, 20, 30};
const std::vector<intCFoo::m_foo(list_of(1)(2)(3));
error: call of overloaded ‘vector(boost::assign_detail::generic_list<int>& )’ is ambiguous
but:
const std::vector<intCFoo::m_foo = list_of(1)(2)(3);
seems ok. I don't understand why - I'd have thought they were equivalent.
--
Lionel B
Lionel B wrote:
std::vector has a constructor that takes a range (i.e. a first and last
iterator) as parameters. These iterators can be pointers, so I suppose
you could use that. Eg.:
#include<vector>
int main()
{
const int a[] = {10,20,30};
const int* const p = a;
const std::vector<intv(p,p+3);
}
works as expected.
Or a bit simpler:
#include<vector>
int main()
{
const int a[] = {10,20,30};
const std::vector<intv(a, a + sizeof(a));
}
Rolf Magnus schrieb:
Or a bit simpler:
#include<vector>
int main()
{
const int a[] = {10,20,30};
const std::vector<intv(a, a + sizeof(a));
}
That's wrong.
You wanted to write
const std::vector<intv(a, a + sizeof(a)/sizeof(*a));
Marcel
On May 2, 6:18 pm, Marcel Müller <news.5.ma...@spamgourmet.comwrote:
Rolf Magnus schrieb:
Or a bit simpler:
#include<vector>
int main()
{
const int a[] = {10,20,30};
const std::vector<intv(a, a + sizeof(a));
}
That's wrong.
You wanted to write
const std::vector<intv(a, a + sizeof(a)/sizeof(*a));
Actually, what he probably wanted to write was:
std::vector< int const v( begin(a), end(a) ) ;
where begin() and end() are the usual thing that everyone pretty
much has in their toolkit:
template< typename T, size_t N >
T*
begin( T (&array)[ N ] )
{
return array ;
}
template< typename T, size_t N >
T*
end( T (&array)[ N ] )
{
return array + N ;
}
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Lionel B wrote:
On Fri, 02 May 2008 14:21:41 +0200, Christian Hackl wrote:
>const std::vector<intCFoo::m_foo(list_of(1)(2)(3));
error: call of overloaded ‘vector(boost::assign_detail::generic_list<int>& )’ is ambiguous
but:
const std::vector<intCFoo::m_foo = list_of(1)(2)(3);
seems ok. I don't understand why - I'd have thought they were equivalent.
Well, they are not exactly equivalent. In the second version, a
temporary std::vector is constructed using list_of(1)(2)(3), and m_foo
is then initialised with that temporary using std::vector's copy
constructor.
So it seems to me that in this example, construction of said temporary
std::vector is necessary to make list_of work, and that the compiler
does not elide the copy constructor call (as it usually does) because
that would change the meaning of the code.
Someone care to shed some light on this issue? :)
--
Christian Hackl This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Christof Krueger |
last post by:
Hello,
I'm quite new to C++ so maybe there's something I miss.
I write a simple board game. It has a board class. This class has a
method that returns the count of pieces a player has on the...
|
by: christopher diggins |
last post by:
I have heard it is considered good practice to pass function parameters as
const& as often as possible, is this true? Is it possible to go overboard?
And if so why? Thanks a lot in advance...
|
by: Last Timer |
last post by:
class A {
public:
A(int a, int b) { this.a=a; this.b=b};
int a;
int b;
}
class B : x(3), y(3), public A(1,3)
{
|
by: Hyoung Lee |
last post by:
A simple method of simulating a class, such as C++ or UML class, in C would
be using the struct construct. Similarly, we use C functions to simulate a
methods; e.g., rv method_a (&struct,...
|
by: ragged_hippy |
last post by:
Hi,
I wanted to create a vector of const references. Something like this:
vector<const x&y;
where x is a class name.
Is this a valid statement? Will this work if I use a reference of 'y'...
|
by: Anjo Gasa |
last post by:
If I have the following class member:
std::vector<floatsingleVector;
I can initialize it to have 50 elements with values of 0.5:
singleVector = std::vector<float>( 50, 0.5 );
My question...
|
by: Rob |
last post by:
How would I do this?
I want to be able to handle vectors of many different types of data
and vectors that can contain any number of other vectors of data.
Currently, I have a templated...
|
by: nw |
last post by:
Hi comp.lang.c++,
I'm using map to store a bunch of vectors. I'd like to be able to
return const references to the vectors from my object. However I can't
see a way of doing this with map, as...
|
by: muzicmakr |
last post by:
I'm porting some code from windows to mac, and there are some
instances of std::vector<const MyType>, that compiled just fine on the
pc, but won't compile under gcc.
I'd never tried to do this...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |