Static Map initialization

Jul 8, 2010 at 8:12pm
Hi ,
I have a strange problem. Currently i am using array of structures and initializing it statically at compile time.

e.g.
const MyStruct elements[] = {
{"abc",VALUE1,5}
{"def",VALUE2,15}
};

MyStruct is a structure with three members.

This way I am able to initialize the "elements" array at compile time.

Now i want the same thing to be achieved by using either vector or map.I know that static initialization will not be possible for vector and map.
But how to do the initialization if there are suppose 100 values to be initialized in "elements".

Jul 8, 2010 at 8:23pm
I don't think there is any other way to do the initialization(s) at compile time but check out Boost.Assign:
http://www.boost.org/doc/libs/1_43_0/libs/assign/doc/index.html

I think C++0x is supposed to have a way to do this at compile time.
Last edited on Jul 8, 2010 at 8:27pm
Jul 8, 2010 at 9:01pm
I don't want to use boost library. What i am doing now is like this

const map<Test_list,Test_info> Create_Test_info_map()
{
map<Test_list,Test_info> Test_info_map;

Test_info meter = {"abc",VALUE1,5};
//m.insert( MapType::value_type(Meters, meter) );
Test_info_map.insert( pair<Test_list,Test_info>(Meters, meter) );
//m[Meters] = meter;
return Test_info_map;
}

I can call this function in one of the main class constructor.
const map<Test_list,Test_info> Test_info_map = Create_Test_info_map();
But i want to keep the Unit_info_map variable global. And calling the above statement in global scope seems a bit ugly to me.
Jul 8, 2010 at 9:21pm
moorecm wrote:
I think C++0x ...

Shouldn't it be called C++1x by now?
Jul 8, 2010 at 9:27pm
If it has to be global, it has to be global. One possible improvement might be to wrap the map in a class, initialize it in its constructor, and make a global instance. That might keep things a little more organized but that's about it.
Last edited on Jul 8, 2010 at 9:28pm
Jul 9, 2010 at 1:37pm
Generally when I have this problem, I solve it this way (you can replace the boost::array<> with
a C-style array):

1
2
3
const boost::array< int, 10 > values_ = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

std::vector<int> v( values_.begin(), values_.end() );

Jul 9, 2010 at 8:07pm
I have applied another method now.
I have defined the following in a header file say test.h

typedef std::map<Test_list, Test_info> Test_info_map;
Test_info meter = {"abc",VALUE1,5};

const Test_info_map::value_type Test_data[] =
{

Test_info_map::value_type(test1,meter ),
Test_info_map::value_type(test2,inches ),
};

const int Num_elems = sizeof Test_data / sizeof Test_data[0];

const Test_info_map Test_table1(Test_data, Test_data + Num_elems);

Now in another cpp file say test.cpp i want to access the elements of Test_table1
Which i am doing like this

for( it=Test_table1.begin(); it!=Test_table1.end(); it++)
{
....
......
}

But compiler gives error in for loop statement that "=" operator is not defined. THis for loop is being executed in another class's member function. I assume this should not be a problem as Test_table1 is global.

Jul 9, 2010 at 8:27pm
You haven't given a type for it?

Also, global variables in header files are doubly bad. Every .cpp file that includes that header,
directly or indirectly, will get its own copy of the data which is both not what you want and
a memory hog.
Jul 9, 2010 at 8:56pm
I found out the issue.Actually because Test_table1 is const i have to use const_iterator instead of normal iterator.
Also i can declare the Test_data[] as static so that only one copy of it is available. Will that be right soln?
Jul 9, 2010 at 9:05pm
If you declare a global variable (not a class member) as static, it will have module-scope.

jsmith wrote:
Also, global variables in header files are doubly bad. Every .cpp file that includes that header,
directly or indirectly, will get its own copy of the data which is both not what you want and
a memory hog.

Actually I think it'll be a multiple definition error once it's included more than once.

I think what you should do, if you want to share the data across files, is to create a source file that contains the global. Then use the extern keyword in a header that will be included elsewhere.
Last edited on Jul 9, 2010 at 9:07pm
Jul 10, 2010 at 3:01am
+1 moorecm
Topic archived. No new replies allowed.