473,396 Members | 1,755 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,396 software developers and data experts.

how to initialize a vector in the constructor

hi, all
Suppose I have the following header file:

#include <vector>
using namespace std;

class Test{
public:
~Test();
Test();
private:
vector<intv;
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

Aug 7 '06 #1
2 21321
* 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<intv;
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>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 7 '06 #2
In article <11*********************@b28g2000cwb.googlegroups. com>,
"zl2k" <kd*******@gmail.comwrote:
hi, all
Suppose I have the following header file:

#include <vector>
using namespace std;

class Test{
public:
~Test();
Test();
private:
vector<intv;
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.
Aug 7 '06 #3

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

Similar topics

15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
2
by: Christian Christmann | last post by:
Hi, I've a header file with the declaration of an int vector: class BSet { private : std::vector<int> data;
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
9
by: www.brook | last post by:
hi, I have a class class A { const int m_a; const int m_b; } m_a can be initialized at the constructor A():m_a(2)
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
9
by: Baby Lion | last post by:
list<intCurve; vector<Curvea; a.reserve(10); then how should I initialize the list in the vector before a.push_back(123) ? what about list<int>Curve ; vector<Curve *a;
9
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want...
11
by: Bob Altman | last post by:
Hi all, I have a class that contains a member variable that is an array of class instances: class MyClass { private: SomeClass m_someClass; SomeClass m_arrayOfClasses; };
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
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 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.