473,396 Members | 2,059 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.

Set boolean array elements to false using STL algorithm?

What STL algorithm do I use to set all bool elements of an array to false in
just one line of code? Thanks!
Jan 1 '06 #1
11 10256

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
| What STL algorithm do I use to set all bool elements of an array to
false in
| just one line of code? Thanks!
|
|

What makes you believe that you need an algorithm to do that?

#include <iostream>
#include <ostream>
#include <vector>

int main()
{
const sz(10);
bool ba[sz] = {false};

std::cout << "bool array with size = " << sz << "\n";
for(int i = 0; i < sz; ++i)
{
std::cout << "ba[" << i << "] = " << ba[i];
std::cout << std::endl;
}

std::vector<bool> vb(10, false);

std::cout << "bool vector with size = " << sz << "\n";
for(int j = 0; j < sz; ++j)
{
std::cout << "ba[" << j << "] = " << ba[j];
std::cout << std::endl;
}

return 0;
}

/*
bool array with size = 10
ba[0] = 0
ba[1] = 0
ba[2] = 0
ba[3] = 0
ba[4] = 0
ba[5] = 0
ba[6] = 0
ba[7] = 0
ba[8] = 0
ba[9] = 0
bool vector with size = 10
ba[0] = 0
ba[1] = 0
ba[2] = 0
ba[3] = 0
ba[4] = 0
ba[5] = 0
ba[6] = 0
ba[7] = 0
ba[8] = 0
ba[9] = 0
*/
Jan 1 '06 #2
>
What makes you believe that you need an algorithm to do that?


Here is why.

class Array {
bool arr[10];
public:
Array() { /* initialise arr */ }
};

What do I put in place of the comment if I want to initialise arr in just a
single line of code?
Jan 1 '06 #3
Jason Heyes wrote:

What makes you believe that you need an algorithm to do that?


Here is why.

class Array {
bool arr[10];
public:
Array() { /* initialise arr */ }
};

What do I put in place of the comment if I want to initialise arr in just a
single line of code?


#include <algorithm>

class Array
{
bool arr[10];
public:
Array()
{
std::fill_n( arr, sizeof(arr)/sizeof(arr[0]), 0);
}
};

Greg

Jan 1 '06 #4

Jason Heyes wrote:

What makes you believe that you need an algorithm to do that?


Here is why.

class Array {
bool arr[10];
public:
Array() { /* initialise arr */ }
};


You can default initialize member arrays using the following syntax:

Array() : arr() { ... }

Jan 1 '06 #5
>
You can default initialize member arrays using the following syntax:

Array() : arr() { ... }


I tried this in a small program and it did not initialise the array.

#include <iostream>
#include <algorithm>
#include <iterator>

class Array
{
int arr[10];
public:
Array() : arr()
{
std::copy(arr, arr + 10, std::ostream_iterator<int>(std::cout,
"\n"));
}
};

int main()
{
Array array;
return 0;
}

The output of the program was this.

-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460

Are you sure you can initialise member arrays in the way you have described?

Jason.
Jan 2 '06 #6
you cannot !
there is no std default contruction of integers, boolean etc.
"arr()" doesn't mean anything here and you have to do a loop

Jan 2 '06 #7
On 2006-01-02 08:47:31 -0500, "ro************@gmail.com"
<ro************@gmail.com> said:
you cannot !
there is no std default contruction of integers, boolean etc.
"arr()" doesn't mean anything here and you have to do a loop


Are you claiming that the following program can print anything other
than the number zero?

#include <iostream>
using namespace std;

struct Foo
{
int i;
Foo() : i() { cout << i << endl; }
};

int main()
{
Foo foo;
}

--
Clark S. Cox, III
cl*******@gmail.com

Jan 3 '06 #8

Jason Heyes wrote:

You can default initialize member arrays using the following syntax:

Array() : arr() { ... }


I tried this in a small program and it did not initialise the array.


It does, refer to the standard §8.5/5.

You are probably using an utterly outdated compiler, such as VC6.
Upgrade to recent one.

Jan 5 '06 #9
>
you cannot !
there is no std default contruction of integers, boolean etc.
"arr()" doesn't mean anything here and you have to do a loop


Are you claiming that the following program can print anything other than
the number zero?

#include <iostream>
using namespace std;

struct Foo
{
int i;
Foo() : i() { cout << i << endl; }
};

int main()
{
Foo foo;
}


There is no default initialisation of integers, boolean, etc, in arrays.
Jan 14 '06 #10
Jason Heyes wrote:
you cannot !
there is no std default contruction of integers, boolean etc.
"arr()" doesn't mean anything here and you have to do a loop

Are you claiming that the following program can print anything other than
the number zero?

#include <iostream>
using namespace std;

struct Foo
{
int i;
Foo() : i() { cout << i << endl; }
};

int main()
{
Foo foo;
}


There is no default initialisation of integers, boolean, etc, in arrays.


The above code should print 0. If you look he uses the default
constructor for i.

int x = int();

*Will* initialize x to 0. Unfortunately, I don't have a copy of the
Standard in front of me, so I can't quote chapter and verse.
Jan 15 '06 #11

ro************@gmail.com wrote:
you cannot !
there is no std default contruction of integers, boolean etc.
"arr()" doesn't mean anything here and you have to do a loop


You are mistaken. Refer to §8.5.

Jan 16 '06 #12

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

Similar topics

18
by: deko | last post by:
I have a counter file that records page hits - each hit is a UNIX timestamp in the file. But I'm only interested in page hits in the last 365 days. The below code creates an array from the file...
14
by: Randell D. | last post by:
Folks, Here's my problem: I am createing an array in a file using the reslut of some PHP and MySQL processing. An example of my array would be examlpe="example one"; examlpe="example...
3
by: Doug Holton | last post by:
I've got a boolean class property that is almost always false. How can I specify that a class property should not be included in the xml serializer output if it is false? I'm using . will never...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
3
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
2
by: Speed | last post by:
Could you please tell me which is the most efficient of way writing a BOOLEAN array to a file. Thanks a ton. Speed
5
by: Manticure | last post by:
Suppose I have a boolean array: boolean b = {true, true, false, false, true, true}; I want to convert it to a decimal number like a binary number. I this case the array represents number...
14
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
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
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
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,...

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.