3

Possible Duplicate:
how-to initialize 'const std::vector<T>' like a c array

What's the proper syntax to provide an explicit initializer for a std::vector when it's a member of a structure? Here's the structure:

struct pattern_info
    {
        std::string pattern;
        std::vector<std::string> patterns;
    };

Here's how I would like to initialize it (the ??? is the part I'm not sure about):

pattern_info p = { "", ??? };

I know that {} will provide a reasonable default for all the structure members, but I prefer not to do that.

When I compile with -std=c++0x I can do this (and it seems to work):

pattern_info p = { "", {} };

I'm using gcc version 4.4.5 (Debian 4.4.5-8) and would like to do this without the -std=c++0x option.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    What do you want to initialize it **to**? If you're happy with the default constructors, you don't need to write anything at all. – Kerrek SB Jul 25 '11 at 12:47
  • @Buster Smith: As per the FAQ, once you've found an answer suitable, you should accept it (it does not have to be the highest voted one) – Sebastian Mach Jul 26 '11 at 07:06
  • @Buster To have this question transfered to your registered account, flag it and ask a moderator to merge your unregistered account with your unregistered account. Be sure to copy-paste the links to both accounts in your flag comment. – Gilles 'SO- stop being evil' Jul 26 '11 at 16:35

3 Answers3

4

Only C++ 0x's std::vector has such initializers. In current C++, you have to use an existing vector, like this ...

std::vector<T> foo;
...push_back...push...push...

const std::vector<T> target = foo;

... or use workarounds like boost::assign.

A second alternative is to use one of std::vector's alternative constructors. One of them is a template and takes an input-iterator range:

const int foo[] = { 1, 1, 2, 3, 5, 8, 13 };
const std::vector<int> bar (foo, 
                            foo + (sizeof foo / sizeof *foo));
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
2

This is only possible in C++0x, as already stated. As an alternative, you could use Boost. Take a look at the example given in the introduction:
> http://www.boost.org/doc/libs/1_39_0/libs/assign/doc/#intro

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
  • OK. Thanks for the help. I'll use the -std=c++0x option to g++ and do this: pattern_info p = { "", {} }; That seems to work and does not produce any compiler warnings even with -Wextra -Wall -Weffc++ – Buster Smith Jul 25 '11 at 13:22
0

If vector is going to be empty (as shown in your c++0x example version) then,

pattern_info p = { "" };

I think, you can even skip to put "" for std::string too. If you want non-empty string then only initialize. To get rid of compiler warning, you may try:

pattern_info p = {};

Edit: If you want to get away with the warnings then provide a default constructor.

struct pattern_info
{
//...
  pattern_info() : pattern(""), patterns(0) {}
};

And declare as it is:

pattern_info p;
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • When I do that, I get this compiler warning: patterns.cpp:76: warning: missing initializer for member ‘pattern_info::patterns’ – Buster Smith Jul 25 '11 at 12:52
  • Have you tried, `pattern_info p = {};` Demo: http://www.ideone.com/wMtsO – iammilind Jul 25 '11 at 12:54
  • Yes, that produces 2 compiler warnings rather than one. It says both the string and the vector are missing initalizers. I compile -Wextra. When I remove that, no compiler warnings are given. But I don't want to remove -Wextra as I'm paranoid. – Buster Smith Jul 25 '11 at 13:00
  • First solution gives this compiler warning: missing initializer for member ‘pattern_info::patterns’ – Buster Smith Jul 25 '11 at 13:06
  • Just write `pattern_info p;`. You'll get an empty string and an empty vector, because the default constructor of `pattern_info` default-constructs the non-POD members. No need to write a constructor. – Steve Jessop Jul 25 '11 at 13:19
  • @iammilind: It was never POD because of std::vector, which isn't POD. http://stackoverflow.com/questions/146452/what-are-pod-types-in-c/146589#146589 . – Sebastian Mach Jul 25 '11 at 13:19
  • @phresnel, edited it. I messed up with `struct` without constructor – iammilind Jul 25 '11 at 13:22