473,238 Members | 1,940 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,238 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 21311
* 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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.