473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Initialising stl containers

Hello!

Sometimes I get the urge to write something like the following template
(see the code below) to help me initialise stl containers. With this
template I could:

vector<int> v = Initializer<int, vector>()(1)(-1)(0);

instead of:

vector<int> v;
v.push_back(1);
v.push_back(-1);
v.push_back(0);

This brings a nice warm feeling to my tummy. Not only is it short and
clear but it seems I can also use this to initialise a static container
in one statement. I could also use it in an intialiser list of a
constructor if I wanted to:

class A
{
public:
A() : v_(Initializer<int, std::vector>()(1)(2)) { }
private:
std::vector<int> v_;
};

But something in the back of my head tells me that my initialiser
template is not 100% kosher.

Has anybody got some insight into this subject? Why haven't I seen a
template like this anywhere, in litterature or in real life projects? Is
there an achilles heel to my approach that I have missed?

Show me the light!

:.:: brasse

/**
* @brief
* This class helps with initialisation of containers.
*
* The following example shows how to decalare and initialise a
* <code>vector</code> of integers in one statement:
*
* @code
* vector<int> v = Initializer<int, vector>()(1)(-1)(0);
* @endcode
*
* This will give the same result as:
*
* @code
* vector<int> v;
* v.push_back(1);
* v.push_back(-1);
* v.push_back(0);
* @endcode
*
* Note: Using <code>Initializer</code> will actually cause the
* objects to be copied two times.
*/
template <typename T, template <typename T> class C>
class Initializer
{
public:
Initializer() { }

Initializer& operator()(const T& v)
{
c.push_back(v);
return *this;
}

operator C<T>()
{
return c;
}

private:
C<T> c;
};
Jul 23 '05 #1
1 1298
Mattias Brändström" <"thebrasse at brasse dot org wrote:
Hello!

Sometimes I get the urge to write something like the following
template (see the code below) to help me initialise stl containers.
With this template I could:

vector<int> v = Initializer<int, vector>()(1)(-1)(0); Has anybody got some insight into this subject? Why haven't I seen a
template like this anywhere, in litterature or in real life projects?
Is there an achilles heel to my approach that I have missed?


Try

http://www.boost.org/libs/assign/doc/index.html

Jonathan
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: phil_gg04 | last post by:
Dear C++ Experts, Over the last couple of months I have been writing my first program using shared memory. It has been something of an "in-at-the-deep-end" experience, to say the least. At...
8
by: Nick Keighley | last post by:
Hi, is there a way of initialising a vector in a similar fashion to initialising an array? consider: Shape* triangle = new Triangle (); Shape* square = new Square (); Shape* shape_arr =...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
2
by: bob | last post by:
Hi, Given: 1) Custom containers that have nothing to do with vector, deque, list, map etc, 2) a custom version of new and delete defined for these containers, customNew and customDelete,...
1
by: Jean-Marc Blaise | last post by:
IBM recommends to place each ts container on a different physical disk. What about the impacts if all containers (DMS) are in the same FS (supported by multiple disks) ? Does DB2 preallocate...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
107
by: DaveC | last post by:
I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
11
by: jimxoch | last post by:
Hi list, Most STL containers are storing their data on the heap. (although some std::string implementations are notable exceptions) Of course, using the heap as storage increases flexibility and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
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...
0
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...
0
marktang
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.