473,407 Members | 2,306 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,407 software developers and data experts.

Initialize vector

Hi,

I've a header file with the declaration of an int vector:

class BSet
{
[snip]
private :
std::vector<int> data;
[snip]

In the source file I'd like to initialize the vector with DEFSIZE copies
of "0" (where DEFSIZE is an int) within a contructor. My idea was to use
the standard vector constructor "vector<T> v(DEFSIZE,'0')" but all my
attempts failed.

I also used to try:

BSet::BSet()
{
for (int i=0; i < DEFSIZE; i++)
{
data[i] = 0;
}
[snip]

but this results in a "Segmentation fault" when accessing the elements
of the vector data.

Any idea how to use the standard constructor appropriately?

Thanks
Chris


Jul 23 '05 #1
2 7301

"Christian Christmann" <pl*****@yahoo.de> wrote in message
news:3a*************@individual.net...
Hi,

I've a header file with the declaration of an int vector:

class BSet
{
[snip]
private :
std::vector<int> data;
[snip]

In the source file I'd like to initialize the vector with DEFSIZE copies
of "0" (where DEFSIZE is an int) within a contructor. My idea was to use
the standard vector constructor "vector<T> v(DEFSIZE,'0')" but all my
attempts failed.

I also used to try:

Why is it that I cannot find any clue to the existence of such a constructor
in the class definition above? I'm assuming that it part of the "[snips]"
above. ;-)
BSet::BSet()


Use an initialization list:
BSet::BSet():data(DEFSIZE, 0)
{
}

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>

Jul 23 '05 #2

"Christian Christmann" <pl*****@yahoo.de> wrote in message
news:3a*************@individual.net...
I also used to try:

BSet::BSet()
{
for (int i=0; i < DEFSIZE; i++)
{
data[i] = 0;
}
[snip]

but this results in a "Segmentation fault" when accessing the elements
of the vector data.
That is because the elements of "data" you are writing above (in the
for-loop) do not exist at that point. Consider using push_back in case you
want to do something on these lines.

--
Sumit Rajan <su*********@gmail.com>

Any idea how to use the standard constructor appropriately?

Thanks
Chris

Jul 23 '05 #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: slack_justyb | last post by:
Hello, I'm trying to figure the best way of doing the following. *I need an array of strings that are globally accessable from within 'arrayhere.h' *I need that array of strings to not change...
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; }
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...
2
by: zl2k | last post by:
hi, all Suppose I have the following header file: #include <vector> using namespace std; class Test{ public: ~Test(); Test();
2
by: heng | last post by:
If the data member of a class is an array, how to initialize? I tried the following, but it is wrong. class A { public: int a; A():a({0,0,0}){} };
16
by: JDT | last post by:
Hi, Can someone show me how to set any integer (or float) in an std::vector as zero in a way other than using a for loop? Can we apply memset() or ZeroMemory() to the vector? Thanks for your...
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;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.