how to initialize a vector in the constructor

Z

zl2k

hi, all
Suppose I have the following header file:

#include <vector>
using namespace std;

class Test{
public:
~Test();
Test();
private:
vector<int> v;
vector<vector<int> > vv;
}

Then in the Test.cpp, I have

Test::Test() : v(0), vv(0){}

Am I initialize the vector correct? At the time when the constructor is
excuted, I don't have the number to fill the vector yet, so I put them
to size of 0. (It seems I initialize them to null, but null is not
initialized, right?) Please correct me if I am doing wrong. I got
strange run time errors which indicating some of the parameters are not
initialized correctly. Thanks for help.

zl2k
 
A

Alf P. Steinbach

* zl2k:
hi, all
Suppose I have the following header file:

#include <vector>
using namespace std;

Don't put 'using namespace std;' in a header file.

It can also be a good idea to restrict its usage elsewhere.

See the FAQ item "Should I use using namespace std in my code?",
currently at <url:
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5>.

class Test{
public:
~Test();
Test();
private:
vector<int> v;
vector<vector<int> > vv;
}

Then in the Test.cpp, I have

Test::Test() : v(0), vv(0){}

Am I initialize the vector correct?

Yes. You initialize both vectors to zero size. I.e. they're empty.

At the time when the constructor is
excuted, I don't have the number to fill the vector yet, so I put them
to size of 0. (It seems I initialize them to null, but null is not
initialized, right?) Please correct me if I am doing wrong. I got
strange run time errors which indicating some of the parameters are not
initialized correctly. Thanks for help.

See the FAQ item "How do I post a question about code that doesn't work
correctly?", currently at <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.
 
D

Daniel T.

"zl2k said:
hi, all
Suppose I have the following header file:

#include <vector>
using namespace std;

class Test{
public:
~Test();
Test();
private:
vector<int> v;
vector<vector<int> > vv;
}

Then in the Test.cpp, I have

Test::Test() : v(0), vv(0){}

Am I initialize the vector correct?

That's one way to do it.
At the time when the constructor is
excuted, I don't have the number to fill the vector yet, so I put them
to size of 0. (It seems I initialize them to null, but null is not
initialized, right?) Please correct me if I am doing wrong. I got
strange run time errors which indicating some of the parameters are not
initialized correctly. Thanks for help.

Chances are, you are trying to access elements in one of the vectors
that don't exist yet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top